打开失败:ENOENT(没有这样的文件或目录)正在缩略图上显示图像

问题描述 投票:0回答:1

我正在尝试在ImageView上显示BitMapImage,但我能够使Camera正常工作,但是从目录访问图像仍会给我E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /external/images/media/15126: open failed: ENOENT (No such file or directory)

我的代码在下面

    private void openImageIntent(){

    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "amfb" + File.separator);
    root.mkdir();
    final String fname = "img_" + System.currentTimeMillis() + ".jpg";
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam){
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }

    //FileSystem
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    startActivityForResult(chooserIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

并且在onActivityForResult中,我检查了结果并设置了图像

selectedImageUri = data == null ? null : data.getData();
                Log.d("ImageURI", selectedImageUri.toString());
                // /Bitmap factory
                BitmapFactory.Options options = new BitmapFactory.Options();
                // downsizing image as it throws OutOfMemory Exception for larger
                // images
                options.inSampleSize = 8;
                final Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri.getPath());
                preview.setImageBitmap(bitmap);

任何帮助将不胜感激。谢谢。

android android-imageview android-bitmap
1个回答
0
投票
我实际上通过在onActivityForResult中执行此操作解决了我的问题,>

selectedImageUri = data == null ? null : data.getData(); Log.d("ImageURI", selectedImageUri.getLastPathSegment()); // Bitmap factory BitmapFactory.Options options = new BitmapFactory.Options(); // downsizing image as it throws OutOfMemory Exception for larger images options.inSampleSize = 8; try { InputStream input = getContentResolver().openInputStream(selectedImageUri); final Bitmap bitmap = BitmapFactory.decodeStream(input); Log.d("ImageURI", input.toString()); preview.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); }

© www.soinside.com 2019 - 2024. All rights reserved.