Android - 获取图像 uri 的问题 [英] Android - Problem getting image uri

查看:30
本文介绍了Android - 获取图像 uri 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题所说的那样,我的问题涉及相机拍摄的图像的图像 uri.Uri 始终为空……为什么?

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);


        }
    }

推荐答案

看看这个,

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.

你需要权限,

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

这篇关于Android - 获取图像 uri 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆