android从内存中的url存储下载图像,然后在imageview中显示该图像
我想从url下载图像并在存储后将其存储到内存中获取存储在内存中的图像并在imageview中显示它您可以使用此代码下载图像android从内存中的url存储下载图像,然后在imageview中显示该图像,android,android-image,android-internal-storage,Android,Android Image,Android Internal Storage,我想从url下载图像并在存储后将其存储到内存中获取存储在内存中的图像并在imageview中显示它您可以使用此代码下载图像 URL url = new URL(<your url>); InputStream in = new BufferedInputStream(url.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024
URL url = new URL(<your url>);
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = in.read(buf);
while (n!=-1)
{
out.write(buf, 0, n);
n=in.read(buf)
}
out.close();
in.close();
byte[] response = out.toByteArray();
其中,内部存储的文件路径为
String filePath=getFilesDir().getPath() + File.separator + "image_" + <some unique identifier like int or string that is different for different images>
希望有帮助。您尝试了什么?我需要在imageview中显示图像,但首先我需要从url下载图像并将其存储到内存中,然后将图像存储在内存中并在imageview中显示
String filePath=getFilesDir().getPath() + File.separator + "image_" + <some unique identifier like int or string that is different for different images>
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}