如何从内部存储器读取图像



我已经将图像下载到我的应用程序数据/数据/包内存中。它已经下载了,但问题是我并没有得到在imageviewer中显示图像的路径。但不能。请告诉我如何展示。请注意路径。非常感谢。这是我的下载代码。

ImageView imageView = (ImageView) findViewById(R.id.iv);
String mUrl = "https://cometonice.com/im.gif";
InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mUrl,
new Response.Listener<byte[]>() {
@Override
public void onResponse(byte[] response) {
// TODO handle the response
try {
if (response != null) {
FileOutputStream outputStream;
String name = "im.gif";
outputStream = openFileOutput(name, Context.MODE_PRIVATE);
outputStream.write(response);
outputStream.close();
Toast.makeText(MainActivity.this, "Download complete.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO handle the error
error.printStackTrace();
}
}, null);
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack());
mRequestQueue.add(request);

你可以这样使用凌空抽射的ImageRequest

ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
saveBitmapToFile(bitmap)
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
}
});

并将其保存为:

public String saveBitmapToFile(Bitmap bitmap) {
FileOutputStream out = null;
String filename = null;
try {
File f = new File(Environment.getExternalStorageDirectory(), "myapp");
if (!f.exists()) {
f.mkdirs();
}
filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myapp/" + UUID.randomUUID().toString() + ".jpg";
out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return filename;
}

最新更新