【问题标题】:(Android) Camera Capture not saving, can't load bitmap of image(Android) 摄像头捕获不保存,无法加载图像的位图
【发布时间】:2017-07-06 21:26:49
【问题描述】:

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

这是相关代码,改编自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)

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

【问题讨论】:

    标签: android image android-bitmap android-fileprovider


    【解决方案1】:
    Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
    

    这行不通。 Uri 不是 File

    getPhotoFileUri()重构为两种方法:

    • getPhotoFile() 生成您的File
    • getPhotoFileUri() 获得 FileProvider UriFile

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

    【讨论】:

    • 非常感谢您的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    相关资源
    最近更新 更多