我正在尝试将我不久前拍摄的照片保存到另一个文件夹中(该文件夹已经存在)。我的清单在我需要的权限上是最新的,并且我知道我需要使用以下内容:
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
以下是我的代码。我做错了什么,它不会保存?谢谢!
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = viewIt.getId();
String root = Environment.getExternalStorageDirectory().toString();
String myDir = new String(root + "/New Directory/Folder 1/");
String description = "";
String filestring = arrPath[id];
try {
MediaStore.Images.Media.insertImage(getContentResolver(), myDir, filestring, description);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Toast.makeText(getApplicationContext(), "Save Successful", Toast.LENGTH_SHORT).show();
}
});
以下是我的 logcat 中的错误:
04-20 17:38:03.763: E/AndroidRuntime(1003): FATAL EXCEPTION: main
04-20 17:38:03.763: E/AndroidRuntime(1003): java.lang.NullPointerException
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:551)
04-20 17:38:03.763: E/AndroidRuntime(1003): at com.test.Import$ImageAdapter$2$4$1.onClick(Import.java:339)
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.view.View.performClick(View.java:2485)
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.view.View$PerformClick.run(View.java:9080)
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.os.Handler.handleCallback(Handler.java:587)
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.os.Looper.loop(Looper.java:123)
04-20 17:38:03.763: E/AndroidRuntime(1003): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-20 17:38:03.763: E/AndroidRuntime(1003): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 17:38:03.763: E/AndroidRuntime(1003): at java.lang.reflect.Method.invoke(Method.java:507)
04-20 17:38:03.763: E/AndroidRuntime(1003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-20 17:38:03.763: E/AndroidRuntime(1003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-20 17:38:03.763: E/AndroidRuntime(1003): at dalvik.system.NativeStart.main(Native Method)