0

I have tried everything but Image is not showing in Gallery, but its saved in Internal Storage. I have tried several tutorials, but unable to solve it .How can I implement it so that it is shown in Gallery

 download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (ContextCompat.checkSelfPermission(FullScreenActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                //to get the image from the ImageView (say iv)
                BitmapDrawable draw = (BitmapDrawable) fullscreenimage.getDrawable();
                Bitmap bitmap = draw.getBitmap();

                FileOutputStream outStream = null;
                File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File(sdCard.getAbsolutePath() + "/IslamicStatusPics");
                Log.e("DIR", String.valueOf(dir));
                dir.mkdirs();
                String fileName = String.format("%d.jpg", System.currentTimeMillis());
                File outFile = new File(dir, fileName);
                try {
                    outStream = new FileOutputStream(outFile);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                try {
                    outStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                AlertDialog alertDialog = new AlertDialog.Builder(FullScreenActivity.this).create();
                alertDialog.setTitle("Success");
                alertDialog.setMessage("Image Saved to IslamicStatusPics Folder Successfully !");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();

                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/storage/emulated/0/IslamicStatusPics/"))));


            //    Toast.makeText(getApplicationContext(), "Image Saved to IslamicStatusPics Successfully !", Toast.LENGTH_SHORT).show();
             //   Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
             //   intent.setData(Uri.fromFile(dir));
             //   sendBroadcast(intent);
            }else {
                requestStoragePermission();
            }

Please help me Guide in the Right Path. Thanks

Zed
  • 57
  • 9
  • use [`MediaScannerConnection`](https://developer.android.com/reference/android/media/MediaScannerConnection) – Pawel Sep 03 '19 at 11:23
  • check this,maybe it will help you - https://stackoverflow.com/a/12688520/7649582 – Asteroid Sep 03 '19 at 11:25

1 Answers1

0

You can try below code with saved file uri:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Intent mediaScanIntent = new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(Uri.fromFile(outFile)); //change here
            activity.sendBroadcast(mediaScanIntent);
        } else {
            activity.sendBroadcast(new Intent(
                    Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36