【问题标题】:File not found exception:java.io.FileNotFoundException: /storage/emulated/0/image.jpg: open failed: ENOENT (No such file or directory)找不到文件异常:java.io.FileNotFoundException:/storage/emulated/0/image.jpg:打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2017-12-05 12:49:01
【问题描述】:

找不到文件异常:java.io.FileNotFoundException:/storage/emulated/0/FolderName/subFolderName/my_image.jpg:打开失败:ENOENT(没有这样的文件或目录)

我只在某些手机上遇到了异常

我在保存从相机捕获的图像时收到文件未找到异常。 这是我的代码

我正在捕捉图像:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                        startActivityForResult(takePictureIntent, 0);
                    }

在 onActivityResult 中:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0)
    {
        if(data!=null)
        {
            Bundle extras = data.getExtras();
            if (extras != null)
            if (img1) {
                imgAdd1.setVisibility(View.GONE);
                Bitmap thumbnail_1 = (Bitmap) extras.get("data");
                Bitmap resized = scaleBitmap(thumbnail_1) ;
                selectedImage1 = data.getData() ;
                //Bitmap thumbnail_1 = (Bitmap) data.getExtras().get("data");
                image1.setVisibility(View.VISIBLE);
                image1.setImageBitmap(thumbnail_1);
                imageTag = 1 ;
                saveImage(resized, (int) System.currentTimeMillis());
                img1 = false ;
                imgDelete1.setVisibility(View.VISIBLE);
            }
}}

我保存图片的功能是:

public void saveImage(Bitmap bitmap, int i){
    try {

        String stored = null;

        File sdCard = Environment.getExternalStorageDirectory();
        File file = new File(sdCard, "/GMC/Images"
                + File.separator + user_id + "_" + imageTag + ".jpg");

        if (file.exists()) {
            file.delete();
        }



        if (isWriteStorageAllowed()) {
            if ( isReadStorageAllowed())
            //file.createNewFile();
            try {

                FileOutputStream out = new FileOutputStream(file);//getting exception at this line
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
                stored = "success";
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (imageTag == 1) {
                selectedImagePath1 = Environment.getExternalStorageDirectory() + "/GMC/Images"
                        + File.separator + user_id + "_" + imageTag + ".jpg";
            } 
        }
        else {
            requestWriteStoragePermission();
            requestStoragePermission();
        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

已经在清单中添加了权限,例如

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA">

还检查了运行时权限:

//We are calling this method to check the permission status
private boolean isReadStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;
    else {
        //If permission is not granted returning false
        return false;}
}

private boolean isWriteStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;
    else {
        //If permission is not granted returning false
        return false;}
}

//Requesting permission
private void requestStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.READ_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_READ_EXTERNAL_STORAGE);
}

//Requesting permission
private void requestWriteStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.WRITE_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_WRITE_EXTERNAL_STORAGE);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    {
        switch (requestCode){
            case PERMISSION_READ_EXTERNAL_STORAGE :
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(context , "Read Permission granted",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(context , "Read Permission Denied" ,Toast.LENGTH_SHORT).show();
                }
                return;
            case  PERMISSION_WRITE_EXTERNAL_STORAGE :
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(context , "Write Permission granted" ,Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(context , "write Permission Denied",Toast.LENGTH_SHORT).show();
                }
                return;
        }

    }
}

检查了链接 java.io.FileNotFoundException: /storage/emulated/0/saved_images/grub.jpg: open failed: ENOENT (No such file or directory)

我在两部不同的手机上测试了代码,一部是联想(Marshmallow),另一部是三星(marshmallow),但它在三星中出现错误,而在联想上完美运行。

会有什么问题? 请帮忙

【问题讨论】:

  • 请提供完整的 Java 堆栈跟踪,以及该堆栈跟踪中引用的代码。请记住,如果您没有权限,则不要写出文件。在这种情况下,当您请求许可时,您不会返回并保存图像。我强烈建议您在启动 ACTION_IMAGE_CAPTURE Intent 之前请求权限。
  • @Ronak.. 未创建文件夹...我现在应该做什么
  • @CommonWare .. 我也在点击事件中请求了权限。即ACTION_IMAGE_CAPTURE 之前
  • @RonakThakkar 所有图像都将进入默认相机文件夹
  • folders are not created...what should i do now, 那么谁应该这样做呢?没有创建这些文件夹的代码。在尝试将文件放入其中之前,您也不会检查这些文件夹是否存在。如果文件夹不存在,请创建它们!

标签: android android-camera


【解决方案1】:

尝试在清单文件中添加largeheap=true。联想手机无法获取大图像,而三星手机能够从图库相机等获取大图像。

【讨论】:

  • 在哪里添加 this..means 是在应用程序标签中..还是在开头??
  • 您应该清楚地指定该属性的放置位置!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多