0

How can change image saving location i have created the folder but how to save image in it. all downloaded images are saved in pictures folder

   @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            ContentResolver r = contentResolverWeakReference.get();
            AlertDialog alertDialog = alertDialogWeakReference.get();
            if (r != null)
                file = new File(Environment.getExternalStorageDirectory().getPath() + "/CreativeGraphy");
            if (!file.exists()) {
                file.mkdir();
            }
            try {
                file.createNewFile();
                MediaStore.Images.Media.insertImage(r, bitmap, name, desc);

            } catch (Exception e) {
                            e.printStackTrace();

            }
            alertDialog.dismiss();
            Toast.makeText(context, "Download succeed ", Toast.LENGTH_SHORT).show();
        }
vinit
  • 39
  • 1
  • 11
  • Possible duplicate of [How to save a bitmap on internal storage](https://stackoverflow.com/questions/15662258/how-to-save-a-bitmap-on-internal-storage) – ADM May 22 '18 at 14:30

2 Answers2

0

Use this method

public static void saveBitmap(String path, Bitmap bitmap) {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

After saving you can call scanFile method to index your file in the gallery.

MediaScannerConnection.scanFile(context, new String[]{path}, null, null);
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
  • @vinit maybe you wrong pass arguments, Path must be a full path to file. For example Environment.getExternalStorageDirectory().getPath() + "/CreativeGraphy/my_image.png" – Dmitriy Puchkov May 22 '18 at 14:46
0

thanks everyone This works

 public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    ContentResolver r = contentResolverWeakReference.get();
    AlertDialog alertDialog = alertDialogWeakReference.get();
    if (r != null)
       file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CreativeGraphy";
    File dir = new File(file_path);
    if (!dir.exists()) {
        dir.mkdir();
    }
    File file = new File(dir,name );
    FileOutputStream fOut;
    try {
        MediaStore.Images.Media.insertImage(r, bitmap, name, desc);
        fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    alertDialog.dismiss();
    Toast.makeText(context, "Download succeed ", Toast.LENGTH_SHORT).show();
}
vinit
  • 39
  • 1
  • 11