【问题标题】:android download the image from url store in internal memory then display that image in imageviewandroid从内部存储器中的url存储下载图像,然后在imageview中显示该图像
【发布时间】:2016-05-31 05:59:45
【问题描述】:

我想从 url 下载图片并在存储后将其存储到内存中获取存储在内存中的图像并将其显示在 imageview 中

【问题讨论】:

  • 你有什么尝试?
  • 我需要在 imageview 中显示图像,但首先我需要从 url 下载图像并将其存储到内存中,然后将图像存储在内存中并在 imageview 中显示

标签: android android-image android-internal-storage


【解决方案1】:

您可以使用此代码下载图像

  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();

下面的代码将其保存到内部存储

  FileOutputStream fos = new FileOutputStream(filePath);
  fos.write(response);
  fos.close();

内部存储的文件路径在哪里

  String filePath=getFilesDir().getPath() + File.separator + "image_" + <some unique identifier like int or string that is different for different images>

并在 imageView 中显示使用

  File imgFile = new  File(filePath);

if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

 }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2012-07-18
    • 2013-01-16
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多