我正在使用运行Marshmallow的Galaxy S6。我遵循了此文档https://developer.android.com/training/camera/photobasics.html,并且可以在我的应用中拍照。但是,我无法将照片添加到图库中。
这是我的代码:
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());
执行后,我在日志中看到以下内容:
06-19 03:09:20.121 8803-8803 / com.my.package.name D / TakePicActivity:发送广播以将图像添加到图库。 06-19 03:09:20.121 8803-8803 / com.my.package.name D / TakePicActivity:contentUri路径:/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
我可以adb shell
和ls -l /storage/emulated/0/Android/data/com.my.package.name/files/Pictures/JPEG_20160619_030913_-155289398.jpg
查看-rw-rw---- u0_a229 sdcard_rw 5004392 2016-06-19 03:09 JPEG_20160619_030913_-155289398.jpg
我的AndroidManifest.xml包含:
<?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>
我有这个BroadcastReceiver,但是在我尝试添加刚刚通过应用程序进入图库的图片后,在logcat中看不到任何内容:
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");
}
}
}