【问题标题】:How can I download image for ImageView in Java, Android?如何在 Java、Android 中下载 ImageView 的图像?
【发布时间】:2019-11-02 20:10:45
【问题描述】:

我需要从我的服务器下载图像并将此图像加载到 imageview 中。所以我有一个问题 - 我可以将图像下载到内存中并将其设置为 ImageView,而不保存在 sdcard/本地存储上吗?或者我必须下载到一些文件存储中?如果可能的话,请给我一个例子。

【问题讨论】:

标签: java android


【解决方案1】:

问了同样的问题,所以你可以搜索,

SAME Question

InputStream in = null;
try
{
    URL url = new URL("URL FOR IMAGE");
    URLConnection urlConn = url.openConnection();
    HttpURLConnection httpConn = (HttpURLConnection) urlConn;
    httpConn.connect();
    in = httpConn.getInputStream();
}
catch (MalformedURLException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
}
bmpimg = BitmapFactory.decodeStream(in);
ImageView iv = "MY IMAGEVIEW";
iv.setImageBitmap(bmimg);

【讨论】:

  • 这应该是一个评论(可能重复)。
  • ohk..thankx..将删除此答案并发表评论..请继续教我 Android 和 StackOverflow...你真的很有帮助。
  • @PareshMayani 如果您对此有所了解,请同时查看此问题stackoverflow.com/questions/7806261/…
【解决方案2】:

这可以帮助你。

Bitmap bmImg;
void downloadFile(String fileUrl){
      URL myFileUrl =null;          
      try {
           myFileUrl= new URL(fileUrl);
      } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
      try {
           HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
           conn.setDoInput(true);
           conn.connect();
           InputStream is = conn.getInputStream();

           bmImg = BitmapFactory.decodeStream(is);
           imView.setImageBitmap(bmImg);
      } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
 }

来源:http://en.androidwiki.com/wiki/Loading_images_from_a_remote_server

也看到这个

http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/

【讨论】:

    【解决方案3】:

    好的,首先将该库实现到应用级别的 gradle 中,以便将图像从 url 加载到图像视图中:

     implementation("com.github.bumptech.glide:glide:4.6.1")
                {
                    exclude group: "com.android.support"
                }
    

    现在在您的活动中编写以下代码以加载图像:

    Glide.with(this).load("url of your image").thumbnail(Glide.with(this).load(R.drawable.loadingsingleimage)).into(imageView);
    

    现在通过执行以下代码将其下载到您的设备存储中..

     download.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
                        Bitmap bitmap = drawable.getBitmap();
    
                        File filepath = Environment.getExternalStorageDirectory();
                        File dir = new File(filepath.getAbsolutePath() + "/MotivationalQuotes/");
                        dir.mkdir();
    
                        File file = new File(dir, System.currentTimeMillis() + ".png");
                        try {
                            outputStream = new FileOutputStream(file);
                        } catch (FileNotFoundException e) {
                            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    
                        }
    
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    
                        try {
                            outputStream.flush();
                        } catch (IOException e) {
                            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        Toast.makeText(getApplicationContext(), "Downloaded into MotivationalQuotes.", Toast.LENGTH_SHORT).show();
                    }
                    catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Image is not loaded yet...", Toast.LENGTH_SHORT).show();
                    }
                }
    
            });
    

    【讨论】:

      【解决方案4】:

      参见here 示例代码。您可以使用BitmapFactory 来实现您的目标:

      HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
      conn.setDoInput(true);
      conn.connect();
      InputStream is = conn.getInputStream();
      
      bmImg = BitmapFactory.decodeStream(is);
      imView.setImageBitmap(bmImg);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-04
        • 2019-02-17
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多