如何从Android的内部存储中检索位图文件?
问题描述:
我使用此代码位图图像保存到Android的内部存储:如何从Android的内部存储中检索位图文件?
public boolean saveImageToInternalStorage(JSONObject jobj, int flag) {
try {
URL url = new URL("http://192.168.43.94" + jobj.get("location"));
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
FileOutputStream fos = null;
// Use the compress method on the Bitmap object to write image to
// the OutputStream
if (flag == 0) {
fos = context.openFileOutput("0_" + jobj.getString("id") + ".png", Context.MODE_PRIVATE);
} else {
fos = context.openFileOutput(jobj.getString("first_category") + "_" + jobj.getString("id") + ".png", Context.MODE_PRIVATE);
}
// Writing the bitmap to the out stream
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
System.out.println("Picture saved successfully");
return true;
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
return false;
}
我怎样才能事后检索位图文件?
答
您可以使用此代码加载位图:
InputStream is = context.openFileInput(filename);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
// and now you can use bitmap