Android 正在尝试将图像保存到库
我是Android开发新手,在尝试将图像保存到图库时遇到了一些困难。我遵循了上的指导原则,在我尝试使用getExternalStoragePublicDirectory将图像保存到手机上的多媒体资料之前,它工作正常 当我使用getExternalFilesDir()时,所有的方法都有效,但是当我尝试使用getExternalStoragePublicDirectory将图片传送到gallery Ii时,出现了NullPointerException。Android 正在尝试将图像保存到库,android,Android,我是Android开发新手,在尝试将图像保存到图库时遇到了一些困难。我遵循了上的指导原则,在我尝试使用getExternalStoragePublicDirectory将图像保存到手机上的多媒体资料之前,它工作正常 当我使用getExternalFilesDir()时,所有的方法都有效,但是当我尝试使用getExternalStoragePublicDirectory将图片传送到gallery Ii时,出现了NullPointerException。 我已将写入外部存储权限添加到清单中 我试过阅
我已将写入外部存储权限添加到清单中 我试过阅读其他关于这个问题的帖子,但还没有找到解决这个问题的办法 这是我的密码。如果有人能帮我找出是什么导致了空点异常,我会很感激的
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 = Environment.getExternalStoragePublicDirectory(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;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private void galleryAddPic() {
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);
}
下面是我在Logcat中的错误:
08-02 20:04:12.920 17455-17455/com.example.android.fishlist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fishlist, PID: 17455
java.lang.NullPointerException
at java.io.File.<init>(File.java:282)
at com.example.android.fishlist.NewCatch.galleryAddPic(NewCatch.java:192)
at com.example.android.fishlist.NewCatch.access$200(NewCatch.java:45)
at com.example.android.fishlist.NewCatch$2.onClick(NewCatch.java:101)
at android.view.View.performClick(View.java:6891)
at android.widget.TextView.performClick(TextView.java:12651)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
08-02 20:04:12.920 17455-17455/com.example.android.fishlist E/AndroidRuntime:FATAL EXCEPTION:main
进程:com.example.android.fishlist,PID:17455
java.lang.NullPointerException
在java.io.File.(File.java:282)
位于com.example.android.fishlist.NewCatch.galleryAddPic(NewCatch.java:192)
位于com.example.android.fishlist.NewCatch.access$200(NewCatch.java:45)
位于com.example.android.fishlist.NewCatch$2.onClick(NewCatch.java:101)
在android.view.view.performClick上(view.java:6891)
在android.widget.TextView.performClick上(TextView.java:12651)
在android.view.view$PerformClick.run(view.java:26083)
位于android.os.Handler.handleCallback(Handler.java:789)
位于android.os.Handler.dispatchMessage(Handler.java:98)
位于android.os.Looper.loop(Looper.java:164)
位于android.app.ActivityThread.main(ActivityThread.java:6938)
位于java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.Zygote$MethodAndArgsCaller.run上(Zygote.java:327)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
您以前是否向用户请求访问其文件的权限?我已添加了“写入外部存储”权限。我必须拥有更多权限吗?如果要请求用户的权限,请查看此链接以了解更多信息谢谢。我去看看。