【问题标题】:ENOENT (No such file or directory)ENOENT(没有这样的文件或目录)
【发布时间】:2018-03-29 13:09:28
【问题描述】:

一切正常 uri 也在获取图像和路径,但是当在第二个活动中返回时,图像未显示在 imageView 中并在 logcat 中显示,请帮助

E/ashish:/storage/emulated/0/Pictures/FotoAula/IMG_20180329_183302.jpg E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/Pictures/FotoAula/IMG_20180329_183302.jpg:打开失败:ENOENT(没有这样的文件或目录)

菜单

**

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

<uses-feature android:name="android.hardware.camera.autofocus" />**

这是第一个活动

camera = (ImageView) findViewById(R.id.takePic);
    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = Uri.fromFile(getOutputMediaFile());
            i.putExtra(MediaStore.EXTRA_OUTPUT, file);

            startActivityForResult(i, CAMERA_REQUEST_CODE);

          //  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

           // startActivityForResult(intent, CAMERA_REQUEST_CODE);

        }
    });

 private static File getOutputMediaFile()
{
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "FotoAula");

    if (!mediaStorageDir.exists()){
        if (!mediaStorageDir.mkdirs()){
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    return new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");
}

private String getPath(Uri uri) throws URISyntaxException {
    final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
    String selection = null;
    String[] selectionArgs = null;
    // Uri is different in versions after KITKAT (Android 4.4), we need to
    // deal with different Uris.
    if (needToCheckUri && DocumentsContract.isDocumentUri(getApplicationContext(), uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            return Environment.getExternalStorageDirectory() + "/" + split[1];
        } else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            uri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
        } else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("image".equals(type)) {
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            selection = "_id=?";
            selectionArgs = new String[]{
                    split[1]
            };
        }
    }
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = {
                MediaStore.Images.Media.DATA
        };
        Cursor cursor = null;
        try {
            cursor = getContentResolver()
                    .query(uri, projection, selection, selectionArgs, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}



/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 */
public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 */
public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAMERA_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {



            File file = getOutputMediaFile();
            String path = null;
            try {
                path = getPath(Uri.fromFile(getOutputMediaFile()));

            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(this, PictureActivity.class);
            intent.putExtra("imgUrl", path.toString());
            startActivity(intent);
        }
    }

这是第二个活动

 Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        Log.e("ashish", bundle.getString("imgUrl") + "");

         path = Uri.parse(bundle.getString("imgUrl"));
        ImageView selfiiii = (ImageView) findViewById(R.id.mySelfie);
        selfiiii.setImageURI(path);
    }

【问题讨论】:

  • 您是否授予了所需的读写存储权限?
  • 是的,我更新了我的问题并看到我授予了所有权限
  • @Rahulrr2602 ...
  • 好的。您的设备在哪个 Android 版本上运行?并且请告诉您的目标版本是什么。代码的哪一部分给你这个错误?
  • 在每个版本中出现此错误

标签: android


【解决方案1】:
  path = getPath(Uri.fromFile(getOutputMediaFile()));

您再次致电getOutputMediaFile()。你稍后再做。所以你得到一个不同的文件名。里面有不同的日期时间。

那个文件不存在。

您应该记住并使用您第一次调用getOutputMediaFile() 时获得的路径。

与相机意图一起使用的那个。

【讨论】:

  • 调用什么?什么是'onActivity?
  • 我再次在 onActivityResult 中调用了该方法,所以我问我是否应该再次调用..我应该为我写什么
  • i m asking i shhuld call again or not .. ???奇怪的问题!我不是说你不应该吗?
猜你喜欢
  • 1970-01-01
  • 2018-07-03
  • 2014-01-12
  • 2023-03-31
  • 2013-07-28
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多