0

I have already tried some code from stack overflow. link mention below

How to save image in android gallery

But that answer is not working on my project. Please help me to find out a solution.

I designed two buttons in the application one for saving images and one for share images.

I have tried a code for download the images.but this code showing me nothing.

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
MediaStore.Images.Media.insertImage(context.getContentResolver(), b ,"image", "image decsription");

Now I want to share the same image with the intent. I tried this code but it is showing me

unable to attach media file

my code is below..

private void createShareIntent(){
        Intent shareIntent=new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM,mImageUrl);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent,"Send "));
    } 

Please help me to find a solution. Thanks in advance.

Anuj Mourya
  • 21
  • 1
  • 9
  • You have two things. Saving an image and sharing an image. It is unclear what goes wrong and what help you ask. A solution for what? Please edit your post. – greenapps Mar 13 '18 at 10:41
  • Didn't get it, which part is not working the saving part or the sharing one? Seems you have 2 different questions. For the first one, did you add the permissions for your app to access the external storage? – Eselfar Mar 13 '18 at 10:55
  • @Eselfar Both are not working. sharing part showing me error Unable to attach media file. – Anuj Mourya Mar 13 '18 at 10:59
  • [Check this thread](https://stackoverflow.com/questions/49156395/cant-create-a-folder-in-gallery-external-storage) see if it works for you – Diogo Silva Mar 13 '18 at 12:03
  • But I want to save the image in the gallery on button click.Becoz I display my images on the imageView. so can you please tell me how can I save that image in gallery. – Anuj Mourya Mar 15 '18 at 08:08

1 Answers1

1

Try This

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

Additionally, in order to refresh the gallery and to view the image there:

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

Also make sure that your app has the storage permission enabled: Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission!

Manifest permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
matin sayyad
  • 575
  • 3
  • 10