0

I'm showing images in my app and I want to add download button after every images when user click on it, image will automatically save to folder. Is it possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Saeed Rehman
  • 165
  • 3
  • 14

5 Answers5

0

You can use Picasso library to display images. Add this below code in your build.gradle dependencies:

compile 'com.squareup.picasso:picasso:2.4.0'

Now use this to display images,You can add this code inside the onClick() method of your button.

File file = new File(imagePath);
if(file.exists()) {
    Picasso.with(context).load(file).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView);
}
else {
    Picasso.with(context).load(imageUrl).skipMemoryCache().placeholder(R.drawable.placeholder).into(yourImageView, new PicassoCallBack(yourImageView,imagePath));
}

The picassoCallBack class will look like this :

public class PicassoCallBack extends Callback.EmptyCallback {
    ImageView imageView;
    String filename;
    public PicassoCallBack(ImageView imageView, String filename) {
        this.imageView = imageView;
        this.filename = filename;
    }
    @Override public void onSuccess() {
        // Log.e("picasso", "success");
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        try {
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos1);
            // FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            File file = new File(filename);
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(baos1.toByteArray());
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onError() {
        Log.e("picasso", "error");
    }
}

Hope it will do your job.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
  • Will this allow users to download the image? – geokavel Oct 17 '15 at 04:44
  • No you should download it by your own, if you need then i can provide that code too. But don't you already have a code fot httpGet? – Hari Krishnan Oct 17 '15 at 04:50
  • His question is about downloading images not displaying them. – geokavel Oct 17 '15 at 04:51
  • He doesnt want it to be downloaded when the picture is loaded. He wants to download it to the user's phone if the user pushes a "Download" button – geokavel Oct 17 '15 at 05:07
  • Ok, The above code will do it. If the image exist in the imagePath, then it will display it in the imageview, else it will download it and save to imagePath, and then display in the imageview. The piccasoCallBack class is for downloading and saving it to the imagePath. It is called only if the image is not in that path. – Hari Krishnan Oct 17 '15 at 05:16
0

If you already have the file saved in your application, copy it to this public folder File imagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

Then use the technique provided here to scan the picture into the Gallery. Now when the user opens the Gallery they'll see the picture.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
geokavel
  • 619
  • 4
  • 12
0
private static void persistImage(Bitmap bitmap, String name) {
    File filesDir = getAppContext().getFilesDir();
    File imageFile = new File(filesDir, name + ".jpg");

    OutputStream os;
    try {
        os = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
    }
}
Akhil Jayakumar
  • 2,262
  • 14
  • 25
0

you can use Glide for download image and i think it is better then picasso because it extends picasso.and for more information please see https://github.com/bumptech/glide. for this you just have to include
compile 'com.github.bumptech.glide:glide:3.6.1'
into dependencies and then simply add this code line

Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);`


where http://goo.gl/gEgYUd is URL to pass.and after using this you have not to maintain cache.

enjoy your code:)

John smith
  • 1,781
  • 17
  • 27
0

I tried Universal Image Loader And Picasso before. you see that you need and witch one is enough to you. also to have better decision read this one and this one. it may help you :

enter image description here

Community
  • 1
  • 1
Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71