gpt4 book ai didi

java - 无法在 android 外部文件目录中创建临时文件

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

我正在关注 Google's Taking Photos Simply教程,但它在创建临时文件时失败,出现 java.io.IOException: open failed: EACCES (Permission denied)

这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pricez.fastshop_android" >
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-feature android:required="true" android:name="android.hardware.camera"/>
<!-- Rest of file... -->
</manifest>

所以,我想至少这是正确的。

这是我 Activity 的相关代码:

private String mCurrentPhotoPath;
private static final int REQUEST_TAKE_PHOTO = 1;

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "/JPEG_" + timeStamp + "_"; // I forgot from where I got this, but it was from some SO question that tried to deal with a similar problem
File absoluteFileDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsoluteFile(); // Same as above
Log.i("createImageFile", "absoluteFileDir.getAbsolutePath(): " + absoluteFileDir.getAbsolutePath()); // I/createImageFile﹕ absoluteFileDir.getAbsolutePath(): /storage/sdcard/Android/data/-redacted-/files/Pictures
File image = File.createTempFile(imageFileName, ".jpg", absoluteFileDir);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}

public void takePhoto(View view) {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show();
return;
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile;
try {
photoFile = createImageFile();
} catch (IOException e) {
Log.e("takePhoto", "IOException", e);
return;
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
// Get the dimensions of the View
ImageView imgv = (ImageView)findViewById(R.id.imgPreview);
int targetW = imgv.getWidth();
int targetH = imgv.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
//
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
imgv.setImageBitmap(bitmap);
}
}

编辑 1:我在 SDK 级别 19 的模拟器上运行它。

编辑 2:

如果我手动删除文件夹,getExternalStorageDirectory 开始正常工作。我让这个问题悬而未决,因为它不是真正的解决方法。如果我的代码中一切正常,我将在几天后关闭。

最佳答案

我不知道为什么(我的猜测是文件锁中的某些东西悄悄地出了可怕的错误),但是当我删除并重新创建文件夹时它开始工作(感谢@greenapps,会如果可以的话,请投票)。此时我能做的就是思考如何以某种方式恢复。

我认为删除文件夹不是真正的解决方案,但此时我必须继续。

此外,这种情况可能不会出现在实际设备中。

关于java - 无法在 android 外部文件目录中创建临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25292171/

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