0

I want lo load the image from url and save it in a file in sd card and then load from file and show on image view.

So I am trying to load image using picasso. But its not showing on image view.

Here is url:

http://xesoftwares.co.in/contactsapi/profile_images/d34b638b93773140eb94d5f03c20237c.jpg

Loading image using picasso.

Picasso.with(MainActivity.this).load(url).into(profileImage);

How to download the image from url. Save it in file in sd card and then load the image from file to show on image view?

Please help. Thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111

4 Answers4

5

Use GLIDE library it automatically store your image into Cache .....

   Glide.with(context).load(url).into(profileImage);

Note:- Only first time you require internet for loading image. When Image load its working on without internet.

EDIT:- use this gradle

compile 'com.github.bumptech.glide:glide:3.7.0'

Output:-

o

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • path to compile library? @sushildlh – Sid Sep 24 '16 at 10:14
  • 1
    picasso is working fine ... may be some OutMerroyExcepton in your device check your LagCat Error..... In question i updated output .... – sushildlh Sep 24 '16 at 10:22
  • I don't get any exception. @sushildlh – Sid Sep 24 '16 at 10:26
  • 1
    check you Logcat care fully ...... try this `Picasso.with(this).load(url).resize(1000, 1000).into(profileImage);` – sushildlh Sep 24 '16 at 10:28
  • can we get the file path stored by picasso? I want to store the file name in database. @sushildlh – Sid Sep 24 '16 at 19:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124192/discussion-between-sid-and-sushildlh). – Sid Sep 26 '16 at 09:39
1

Add :

<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" /> 

In your manifest file, otherwise the source cannot be downloaded.

If you want to save it in sd card you need another permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In order to save it.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If you want to get that file in the future.

Next if you want to save it to an external directory, you can try this:

    public void setUpDirectory(String folderName,String source){

        File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+folderName);

        if(!directory.exists() && !directory.isDirectory()) {
            directory.mkdirs();
        }

        URL imageurl = new URL(source); 
        Bitmap bitmap = BitmapFactory.decodeStream(imageurl.openConnection().getInputStream());
        saveFile(directory,bitmap);
    }


private void saveFile(File fileName,Bitmap bmp){


FileOutputStream outputStream = null;
try {
    outputStream = new FileOutputStream(fileName);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, outputStream ); // 100 will be  ignored
} catch (Exception e) {
    e.printStackTrace();
} 
finally {
    try {
        if outputStream != null) {
            outputStream .close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}


}
mariuss
  • 1,177
  • 2
  • 14
  • 30
0

To save it into sd card you must declare uses permissions write read from sd card in manifest, also ask user for these permissions for android 5.0+. All this is documented clearly in android developers official website. Change MainActivity.class to getApplicationContext()

rei koleci
  • 68
  • 1
  • 7
0

If you have put an image as background of ImageView in xml remove it.

rei koleci
  • 68
  • 1
  • 7