(Android)Camera Capture没有保存,无法加载图像的位图

时间:2017-07-06 21:26:49

标签: android image android-bitmap android-fileprovider

我尝试编写启动相机应用程序的代码,保存图像,然后显示捕获图像的位图。

这是相关代码,改编自https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media

    public static final int PICTURE_REQUEST_CODE = 1034;
    public static final String FILE_BEGIN = "note";
    private int note_id = 4;
    public static final String FILE_END = ".jpg";
    private static final String APP_TAG = "School_App";

    private void takePicture() {
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(FILE_BEGIN + note_id + FILE_END));
    if (i.resolveActivity(getPackageManager()) != null){
        startActivityForResult(i, PICTURE_REQUEST_CODE);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICTURE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Uri takenPhotoUri = getPhotoFileUri(FILE_BEGIN + note_id + FILE_END);
            Log.d("NotesDetail", takenPhotoUri.toString());
            // by this point we have the camera photo on disk
            Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
            // RESIZE BITMAP, see section below
            // Load the taken image into a preview
            ImageView ivPreview = (ImageView) findViewById(R.id.iv_notes_picture);
            ivPreview.setImageBitmap(takenImage);
        } else { // Result was a failure
            Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
        }
    }
}

private Uri getPhotoFileUri(String fileName) {
    if(isSpaceAvailable()){
        File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);

        if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
           Log.d(APP_TAG, "Failed to make directory");
        }

        File file = new File(mediaStorageDir.getPath() + File.separator + fileName);

        Uri uri = FileProvider.getUriForFile(NotesDetail.this, "com.kfode.schoolapp.fileprovider", file);
        Log.d("getPhotoFileUri", uri.toString());
        return uri;
    }
    return null;
}

private boolean isSpaceAvailable() {
    String state = Environment.getExternalStorageState();
    return state.equals(Environment.MEDIA_MOUNTED);
}

我还设置了以下权限:

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

在清单的应用程序标签内......

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.kfode.schoolapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"
            />
    </provider>

在file_paths.xml

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="images"
    path="Pictures"/>

当我运行应用程序时,我收到以下日志消息和异常:

D/getPhotoFileUri: content://com.kfode.schoolapp.fileprovider/images/School_App/note4.jpg
D/NotesDetail: content://com.kfode.schoolapp.fileprovider/images/School_App/note4.jpg
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /images/School_App/note4.jpg: open failed: ENOENT (No such file or directory)

不确定这里发生了什么。请帮忙吗?

1 个答案:

答案 0 :(得分:0)

Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());

这不起作用。 Uri不是File

getPhotoFileUri()重构为两种方法:

  • getPhotoFile()生成您的File
  • getPhotoFileUri()获取FileProvider
  • Uri File

然后,使用图像加载库(Glide,Picasso等)从ImageView返回的文件中填充getPhotoFile()。这不仅可以为您处理所有BitmapFactory内容,而且还可以在后台线程上执行此操作,因此您不会像在当前代码中那样冻结UI。