简体   繁体   English

如何在 Java、Android 中为 ImageView 下载图像?

[英]How can I download image for ImageView in Java, Android?

I need to download image from my server and load this image into imageview.我需要从我的服务器下载图像并将此图像加载到 imageview 中。 So I have a question - can I download image into memory and set it for ImageView, without saving on sdcard/local storage?所以我有一个问题 - 我可以将图像下载到内存中并将其设置为 ImageView,而无需保存在 SD 卡/本地存储上吗? Or I must download into some file storage?或者我必须下载到某个文件存储中? Give me example please if it possible.如果可能的话,请给我例子。

same question are asked in SO you can search for that,同样的问题在所以你可以搜索,

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

Ok first of all implement that library into gradle of app level for load the image from url into image view:好的,首先将该库实现到应用程序级别的 gradle 中,以便将图像从 url 加载到图像视图中:

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

Now write below code in your activity for load the image::现在在您的活动中编写以下代码以加载图像:

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

Now download it into the your device storage by implementing the following code..现在通过实现以下代码将其下载到您的设备存储中..

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

        });

This could help you.这可以帮助你。

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

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

See this too也看到这个

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

See here for example code.有关示例代码,请参见此处 You can use BitmapFactory to achieve your goal:您可以使用BitmapFactory来实现您的目标:

HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();

bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中的滚动视图中将图像锚定到Imageview中的中心? - How can I anchor an image to the center within an Imageview in a scrollview, in Android? Java Android如何使ImageView变暗 - Java Android how can I make ImageView darker onClick 将图像从服务器下载到android并在imageview中显示 - Download a image from server to android and show in imageview 从 URL android 下载 imageview 中加载的图像 - Download image loaded in imageview from URL android 如何下载图像并将其放入 Android 的视图中 - How can I download and put an image in a view in Android Android Java:在imageview旋转图像 - Android java : rotate image at imageview Java FXML:为什么我的imageView没有显示,如何从计算机上载图像? - Java FXML: Why is my imageView not Showing, and how can I upload an image from my computer? 如何在ImageView-> LinearLayout android下获取R.drawable.image_sample? - How can I get the R.drawable.image_sample under ImageView -> LinearLayout android? 如何在这个简单的 Android 应用程序中正确使用 Canvas 类将图像显示到 ImageView 中? - How can I correctly use the Canvas class to show an image into an ImageView in this simple Android app? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM