I'm trying to save my edited image into Gallery from where user can access it. I have used "Add the photo to the Gallery" from reference: https://developer.android.com/training/camera/photobasics.html
Here is my Code:
String mCurrentPhotoPath;
public void startSave() {
FileOutputStream fileOutputStream = null;
File new_file = null;
try {
new_file = createImageFile();
fileOutputStream = new FileOutputStream(new_file);
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
Toast.makeText(this, "save Image Success", Toast.LENGTH_SHORT).show();
fileOutputStream.flush();
fileOutputStream.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
I have also changed AndroidManifest.xml
like this and given permission to write in external storage and granted Uri permission.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
I have added xml file paths according to this:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/com.sparkexel.blurd1/files/Pictures" />
</paths>
I can't get my head around what is the problem here. First, there was an error message about external permission or URI permission not granted. I have set URI permission after that. Finally, There is no error message in console. But I can't save my image into the gallery. I have logged path of my saved file. and it is:
/storage/emulated/0/Android/data/com.mypackage/files/Pictures/JPEG_20171216_171109_2682421804312420480.jpg
From that, I assumed that file is saving successfully. But it does not show up in Gallery. I have tried many solutions but nothing works. Please help me.
The File Will be Displayed only if after a Successful Completion of media Scanner.
Note
Save the File To the External Storage . Now the Path is Android Data Folder. Most of the time the Files inside the data Folder wont be Scanned By Media Scanner. For that use
Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Foldername/"
as Folder PathEnvironment.getExternalStorageDirectory().getAbsolutePath()
will Return the ExternalStorge as the PathRun Media Scanner
Also Refer Trigger Media Scanner Programattically