Android-出现图像URI的问题[英] Android - Problem getting image uri

本文是小编为大家收集整理的关于Android-出现图像URI的问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

标题说我的问题涉及相机拍摄的图像的图像URI. Uri总是无效的...为什么?

@Override
public void onClick(View v) {

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);          
    startActivityForResult(cameraIntent, TAKE_PICTURE); 


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

        if (requestCode == TAKE_PICTURE)
        {
            //Uri contentURI = Uri.parse(data.getDataString());
            thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image = (ImageView) findViewById(R.id.photoResultView);  
            image.setImageBitmap(thumbnail);

                photoUri = data.getData();
                Log.e("FOTOLOG",""+ photoUri);


        }
    }

推荐答案

看这个,

android Action_image_capture

可能会为您提供一些见解.

public void startCamera() {

    File photo = null;
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {
        photo = new File(android.os.Environment
                .getExternalStorageDirectory(), FILE_NAME);
    } else {
        photo = new File(getCacheDir(), FILE_NAME);
    }    
    if (photo != null) {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        selectedImageUri = Uri.fromFile(photo);
        startActivityForResult(intent, IMAGE_CAPTURE);
    }
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == IMAGE_CAPTURE) {

        if (resultCode == Activity.RESULT_OK) {

            try {
                Uri selectedImage = selectedImageUri;
                //getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap;
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);
                rptImage.setImageBitmap(bitmap);

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }

        } else {
            selectedImageUri = null;
            rptImage.setImageBitmap(null);
        }

    } 

}

这是我应用程序中的代码示例.

您需要许可,

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

本文地址:https://www.itbaoku.cn/post/102402.html

问题描述

like the title says my problem concerns the image uri of images taken by the camera. Uri is always null ... WHY?

@Override
public void onClick(View v) {

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);          
    startActivityForResult(cameraIntent, TAKE_PICTURE); 


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

        if (requestCode == TAKE_PICTURE)
        {
            //Uri contentURI = Uri.parse(data.getDataString());
            thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image = (ImageView) findViewById(R.id.photoResultView);  
            image.setImageBitmap(thumbnail);

                photoUri = data.getData();
                Log.e("FOTOLOG",""+ photoUri);


        }
    }

推荐答案

Look at this,

Android ACTION_IMAGE_CAPTURE Intent

might provide you with some insight.

public void startCamera() {

    File photo = null;
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {
        photo = new File(android.os.Environment
                .getExternalStorageDirectory(), FILE_NAME);
    } else {
        photo = new File(getCacheDir(), FILE_NAME);
    }    
    if (photo != null) {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        selectedImageUri = Uri.fromFile(photo);
        startActivityForResult(intent, IMAGE_CAPTURE);
    }
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == IMAGE_CAPTURE) {

        if (resultCode == Activity.RESULT_OK) {

            try {
                Uri selectedImage = selectedImageUri;
                //getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap;
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);
                rptImage.setImageBitmap(bitmap);

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }

        } else {
            selectedImageUri = null;
            rptImage.setImageBitmap(null);
        }

    } 

}

That's a code sample from my application.

You need the permission,

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