I am using Galaxy S6 running Marshmallow. I followed this documentation https://developer.android.com/training/camera/photobasics.html and am able to take photos in my app. However, I cannot get the photos to be added to Gallery.
Here is my code to do so:
Log.d(TAG, "sending broadcast to add image to gallery.");
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
Log.d(TAG, "scheme: " + contentUri.getScheme().equals("file"));
Log.d(TAG, "action: " + Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
Log.d(TAG, "path: " + contentUri.getPath());
Log.d(TAG, "ExternalStorageDirectory: " + Environment.getExternalStorageDirectory().getPath());
Once executed I see the following in the logs:
06-19 03:09:20.121 8803-8803/com.my.package.name D/TakePicActivity: sending broadcast to add image to gallery. 06-19 03:09:20.121 8803-8803/com.my.package.name D/TakePicActivity: contentUri path: /storage/emulated/0/Android/data/com.my.package.name/files/Pictures/JPEG_20160619_030913_-155289398.jpg 06-19 03:09:20.131 8803-8803/com.my.package.name D/TakePicActivity: scheme: true 06-19 03:09:20.131 8803-8803/com.my.package.name D/TakePicActivity: action: true 06-19 03:09:20.131 8803-8803/com.my.package.name D/TakePicActivity: path: /storage/emulated/0/Android/data/com.my.package.name/files/Pictures/JPEG_20160619_030913_-155289398.jpg 06-19 03:09:20.131 8803-8803/com.my.package.name D/TakePicActivity: ExternalStorageDirectory: /storage/emulated/0
I am able to adb shell
and ls -l /storage/emulated/0/Android/data/com.my.package.name/files/Pictures/JPEG_20160619_030913_-155289398.jpg
and see -rw-rw---- u0_a229 sdcard_rw 5004392 2016-06-19 03:09 JPEG_20160619_030913_-155289398.jpg
My AndroidManifest.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.package.name">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.SpringboardActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.my.app.name.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<receiver android:name=".MediaScannerBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_STARTED" />
<data android:scheme="file" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_FINISHED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
</manifest>
And I have this BroadcastReceiver but see nothing from it in the logcat after I try to add a pic that was just taken thru the app to gallery:
public class MediaScannerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
Log.d("MediaScanner", "ACTION_MEDIA_SCANNER_STARTED received");
}
if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
Log.d("MediaScanner", "ACTION_MEDIA_SCANNER_FINISHED received");
}
}
}