gpt4 book ai didi

java - Android 没有此类文件或目录异常

转载 作者:行者123 更新时间:2023-12-02 02:25:38 25 4
gpt4 key购买 nike

我正在尝试遵循本教程 https://developer.android.com/training/camera/photobasics.html#TaskPath用于拍摄图像并保存文件。它打开相机,我拍照,单击复选标记,然后它崩溃了。我在 StackOverflow 上找到了几篇文章,并尝试了所有我能理解的解决方案(分别是 file.mkdirs()file.getParentFile().mkdirs() )。

看来,即使它是一个 RuntimeException,但根本问题是 IOException(我捕获并打印消息)。我正在使用 Genymotion,不确定这是否相关。我错过了什么导致我无法创建文件?

我已向我的 list 添加了权限:

<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我的按钮点击:

public void onClick(View v) {
Log.d("DebugMySocks", "Change photo button clicked");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
// CHANGED THIS
mImageUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + "socks", createImageFile());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
} catch (Exception e) {
Log.d("DebugMySocks", "Error creating file " + e.getMessage());
}
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}

以及问题代码(我认为):

  private File createImageFile() throws IOException {
//ADDED THIS
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
5);
}
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStorageDirectory();
Log.d("DebugMySocks", "Storage directory is: " + storageDir);
File image = new File(Environment.getExternalStorageDirectory() + File.separator + "socks"+ File.separator + imageFileName);
Log.d("DebugMySocks", "getAbsoluteFile: " + image.getAbsoluteFile());
image.getParentFile().mkdirs();
image.mkdirs();
image.createNewFile();
//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;
}

LogCat 的输出:

12-14 08:05:01.411 4967-4967/com.example.kevin.moresocksplease D/DebugMySocks: Change photo button clicked
12-14 08:05:01.412 4967-4967/com.example.kevin.moresocksplease D/DebugMySocks: Storage directory is: /storage/emulated/0
12-14 08:05:01.413 4967-4967/com.example.kevin.moresocksplease D/DebugMySocks: getAbsoluteFile: /storage/emulated/0/socks/JPEG_20171214_080501_
12-14 08:05:01.416 4967-4967/com.example.kevin.moresocksplease D/DebugMySocks: Error creating file No such file or directory

异常(exception)情况:

FATAL EXCEPTION: main
Process: com.example.kevin.moresocksplease, PID: 4967
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=111, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.kevin.moresocksplease/com.example.kevin.moresocksplease.EditProfileActivity}: java.lang.NullPointerException: uri
at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:111)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1714)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1693)
at com.example.kevin.moresocksplease.EditProfileActivity.onActivityResult(EditProfileActivity.java:254)
at android.app.Activity.dispatchActivityResult(Activity.java:6932)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
at android.app.ActivityThread.-wrap20(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

最后,包括因为它位于异常堆栈中:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("DebugMySocks", "onActivityResult in EditProfileActivity");
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == this.RESULT_OK) {
this.getContentResolver().notifyChange(mImageUri, null); // <-- THIS IS LINE 254!
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
try
{
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
mProfileImage.setImageBitmap(bitmap);
saveToFirebase(bitmap);
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d("DebugMySocks", "Failed to load", e);
}
}
}

我从中获取信息的其他链接:

File.createNewFile() thowing IOException No such file or directory

Android Camera Intent: how to get full sized photo?

编辑1:

查看此内容后,我将日志添加到 mkdir 请求中,它们都返回 false。根据这位先生的说法,这意味着目录已经存在,所以只是文件的创建失败了? Creating a directory in /sdcard fails

Log.d("DebugMySocks", "parentfile mkdirs: " + image.getParentFile().mkdirs());
Log.d("DebugMySocks", "image.mkdirs: " + image.mkdirs());
Log.d("DebugMySocks", "create file: " + image.createNewFile());

DebugMySocks: parentfile mkdirs: false
DebugMySocks: image.mkdirs: false
DebugMySocks: Error creating file No such file or directory

编辑2:我现在有:

public void onClick(View v) {
Log.d("DebugMySocks", "Change photo button clicked");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED) {
mImageUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + "socks", createImageFile());
} else {
Log.v("DebugMySocks","Permission is revoked");
ActivityCompat.requestPermissions(getParent(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
} catch (Exception e) {
Log.d("DebugMySocks", "Error creating file " + e.getMessage());
}
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0:
boolean permissionsGranted = true;
if (grantResults.length > 0 && permissions.length==grantResults.length) {
for (int i = 0; i < permissions.length; i++){
if (grantResults[i] != PackageManager.PERMISSION_GRANTED){
permissionsGranted=false;
}
}

} else {
permissionsGranted=false;
}
if(permissionsGranted){
try {
mImageUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + "socks", createImageFile());
} catch (Exception e) {
Log.d("DebugMySocks", "Permissions granted but exception happened " + e.getMessage());
}
}
break;
}
}

但是我得到创建文件时出错尝试调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)'在空对象引用上

最佳答案

您需要检查用户是否授予了权限。

private void checkPermission(){
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED) {
createFile();
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}


@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0:
boolean permissionsGranted = true;
if (grantResults.length > 0 && permissions.length==grantResults.length) {
for (int i = 0; i < permissions.length; i++){
if (grantResults[i] != PackageManager.PERMISSION_GRANTED){
permissionsGranted=false;
}
}

} else {
permissionsGranted=false;
}
if(permissionsGranted){
createFile();
}
break;
}
}

private void createFile(){
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
try{
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
mProfileImage.setImageBitmap(bitmap);
saveToFirebase(bitmap);
}catch (Exception e){
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d("DebugMySocks", "Failed to load", e);
}
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("DebugMySocks", "onActivityResult in EditProfileActivity");
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == this.RESULT_OK) {
this.getContentResolver().notifyChange(mImageUri, null); // <-- THIS IS LINE 254!
checkPermission();
}
}

关于java - Android 没有此类文件或目录异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com