android – 无法将从相机拍摄的图像加载到ImageView中

我能够将从相机拍摄的图像保存到内部存储器中,但是我无法将其加载到仍为空的
ImageView中.我已阅读所有相关建议但未找到合适的解决方案.请在下面找到相关代码,任何帮助都会受到很多赞赏,因为我几天都在努力…

清单权限:

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

XML上的ImageView:

<ImageView
    android:id="@+id/recipeImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_weight="1"
    android:background="@color/backgroundColorHomeBottomLayout"
    android:padding="30dp" />

相关活动:

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

    if (requestCode == REQUEST_IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK) {
            setPic();
            //mImageView.setImageURI(fileUri);
        }
        else if (resultCode == RESULT_CANCELED) {
            /** user cancelled Image capture */
            Toast.makeText(this,
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            /** failed to capture image */
            Toast.makeText(this,
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

/** Internal memory methods */
private void launchCameraIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        /** Ensure that there's a camera activity to handle the intent */
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        /** Create the File where the photo should go */
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "v3.com.mycookbook5.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

/** return a unique file name for a new photo using a date-time stamp */
private File createImageFile() throws IOException {
    /** Create an image file name */
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    /** Save a file: path for use with ACTION_VIEW intents */
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.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);
    mImageView.setImageBitmap(bitmap);
}

日志:

07-03 12:56:07.188 3950-16091/? E/Drive.UninstallOperation: Package still installed v3.com.mycookbook5
07-03 12:56:35.350 19680-19680/v3.com.mycookbook5 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Android/data/v3.com.mycookbook5/files/Pictures/JPEG_20160703_125629_-664091716.jpg: open failed: ENOENT (No such file or directory)
07-03 12:56:35.350 19680-19680/v3.com.mycookbook5 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Android/data/v3.com.mycookbook5/files/Pictures/JPEG_20160703_125629_-664091716.jpg: open failed: ENOENT (No such file or directory)

最佳答案 我相信你的问题是因为你没有给图像保存到SD卡所需的时间,因为代码在它完全保存之前就开始寻找它,所以你必须提供一个延迟的处理程序以确保图像成功保存,也使用以下方式获取图像路径:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            setPic();
        }
    }, 750);

并从SD卡获取图像:

File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image_name.jpg");
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ 

    /*
     * here you set all of your bmOptions specs
     */

    return BitmapFactory.decodeFile(path, bmOptions);
}
点赞