我试图将我前一段时间拍摄的照片保存到另一个文件夹(该文件夹已存在)。我的清单是我需要的权限的最新版本,我知道我需要使用以下内容:
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)
答案 0 :(得分:0)
1)带空格的文件夹名称可能有问题,因此请确保检查文件夹是否存在,否则创建它。
File file = new File(myDirPath);
if(!file.exists()) {
if(!file.mkdirs()){
Log.e("TAG", "Error: You can't create folder");
}
}
2)可能有错误,但您没有看到它,因为您捕获异常但不打印它。
尝试替换此
e.printStackTrace();
带
Log.e(“TAG”,“错误:”+ e.toString());
答案 1 :(得分:-1)
我明白了!我决定采用不同的路线,只是重命名文件。我意识到我不想要相同图像的倍数(试图保存SD卡空间)所以我这样做了:
int id = viewIt.getId();
File sdcard = Environment.getExternalStorageDirectory();
File sdddcard = new File(sdcard + "/New Directory/Folder 1/");
File from = new File(arrPath[id]);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String filename = "ImageImport-"+ n +".jpg";
File to = new File(sdddcard,filename);
from.renameTo(to);