0

I need to download image from my server and load this image into imageview. So I have a question - can I download image into memory and set it for ImageView, without saving on sdcard/local storage? Or I must download into some file storage? Give me example please if it possible.

user975290
  • 181
  • 2
  • 6
  • 15
  • possible duplicate of [How to download a image from URL in App](http://stackoverflow.com/questions/7763841/how-to-download-a-image-from-url-in-app) – Paresh Mayani Oct 18 '11 at 13:15

4 Answers4

4

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);
Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • This should be as a comment(Possible duplicate). – Paresh Mayani Oct 18 '11 at 13:15
  • ohk..thankx..will delete this answer and post as comment .. please keep teaching me Android and StackOverflow...You are really helpful. – MKJParekh Oct 18 '11 at 13:18
  • @PareshMayani if you know anything about this please see this question also http://stackoverflow.com/questions/7806261/strange-behavior-of-android-videoview-cant-play-video/7806881#7806881 – MKJParekh Oct 18 '11 at 13:22
0

Ok first of all implement that library into gradle of app level for load the image from url into image view:

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

        });
GIRIRAJ NAGAR
  • 181
  • 1
  • 5
0

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

See this too

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

gtiwari333
  • 24,554
  • 15
  • 75
  • 102
-1

See here for example code. You can use BitmapFactory to achieve your goal:

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

bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194