2

I am taking a screenshot and saving it in storage with my application name. It's not giving any error and saving photo in storage, but it appears in the gallery after I restart my phone.

My save method for photo is like below:

public void saveQuoteImage(Bitmap quoteImage){
  Date now = new Date();
  android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
  try {
    // image naming and path  to include sd card  appending name you choose for file

    String dirPath=Environment.getExternalStorageDirectory().toString()+File.separator+"quotes_king";

    File dirFile=new File(dirPath);
    if(!dirFile.exists())dirFile.mkdirs();

    // String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
    // create bitmap screen capture
    File imageFile = new File(dirFile.getPath()+File.separator+ now + ".jpg");

    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    quoteImage.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);

    outputStream.flush();
    outputStream.close();

  } catch (Throwable e) {
    // Several error may come out with file handling or OOM
    e.printStackTrace();
  }
}

What I am missing in this ?

Thanks

beetstra
  • 7,942
  • 5
  • 40
  • 44
Rajubhai Rathod
  • 501
  • 2
  • 5
  • 14

3 Answers3

4

You just have to tell at MediaScanner that your file exists.

Something like this:

public final void addManually(File file) {
        try{
            MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
                    file.getAbsolutePath(), file.getName(), null);
            mContext.sendBroadcast(new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                    Uri.fromFile(file))
            );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
River
  • 8,585
  • 14
  • 54
  • 67
Erik Minarini
  • 4,795
  • 15
  • 19
0

You should invoke the media scanner on the new file.

how to run media scanner in android

Community
  • 1
  • 1
greenapps
  • 11,154
  • 2
  • 16
  • 19
0

Use media scanner in android.Just add the code below.

MediaScannerConnection.scanFile(this, new String[] { imagefile.toString() }, null, null);
NIKHIL SONI
  • 84
  • 1
  • 1