Save image from ImageView to device gallery

I am trying to save image from ImageView to device gallery. I have tried this code

Editing the code:

    URL url = new URL(getIntent().getStringExtra("imageURL"));
    File f  = new File(url.getPath());

    addImageToGallery(f.getPath(), this);

    public static void addImageToGallery(final String filePath, final Context context) 
    {

       ContentValues values = new ContentValues();

       values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
       values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
       values.put(MediaStore.MediaColumns.DATA, filePath);

       context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }

      

but it requires a file path which I do not have, since I am loading the file from a url. How to save image from ImageView to gallery?

thank..

+3


source to share


3 answers


Plain:

use this code:

//to get the image from the ImageView (say iv)
BitmapDrawable draw = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = draw.getBitmap();

FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();

      

Also, to update the gallery and view the image:

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(file));
    sendBroadcast(intent);

      



Also make sure storage permission is enabled in your application:

Go to "Device Settings"> "Device"> "Applications"> "Application Manager"> "Your Application"> "Permissions"> "Allow Access to Storage"!

Manifest permissions:

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

      

+9


source


ImageView iv = (ImageView)findViewById(R.id.your_image_view);

      

Then set the image and when you want to restore / save it

iv.buildDrawingCache();

Bitmap bmp = iv.getDrawingCache();

      



Then save as usual in the gallery

    File storageLoc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //context.getExternalFilesDir(null);

    File file = new File(storageLoc, filename + ".jpg");

    try{
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();

        scanFile(context, Uri.fromFile(file));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    private static void scanFile(Context context, Uri imageUri){
        Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        scanIntent.setData(imageUri);
        context.sendBroadcast(scanIntent);

    }

      

and of course, make sure your manifest has write permissions to external storage.

+1


source


U can get file from url easily

File f = new File(url.getPath());

      

-1


source







All Articles