android读取Assets图片资源保存到SD卡实例
程序员文章站
2023-01-02 08:58:58
复制代码 代码如下: public class readbitmap { public void readbyte(context c, string name, int...
复制代码 代码如下:
public class readbitmap {
public void readbyte(context c, string name, int indexint) {
byte[] b = null;
int[] intarrat = c.getresources().getintarray(indexint);
try {
assetmanager am = null;
am = c.getassets();
inputstream is = am.open(name);
for (int i = 0; i < intarrat.length; i++) {
b = new byte[intarrat[i]];
// 读取数据
is.read(b);
savemybitmap(bytes2bimap(b), mainactivity.dir+name+i+".jpg");
}
is.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
public static bitmap bytes2bimap(byte[] b) {
if (b.length != 0) {
return bitmapfactory.decodebytearray(b, 0, b.length);
} else {
return null;
}
}
public static boolean savemybitmap(bitmap bmp, string path) {
file f = new file(path);
try {
f.createnewfile();
fileoutputstream fout = new fileoutputstream(f);
bmp.compress(bitmap.compressformat.jpeg, 100, fout);
fout.flush();
fout.close();
return true;
} catch (exception e) {
// todo: handle exception
e.printstacktrace();
}
return false;
}
}