从相机Intent中检索图像URI并将Image设置为Imageview

时间:2017-01-25 12:35:30

标签: java android android-intent

我正在尝试检索图像URI并将图像设置为Imageview ...但每次尝试时,图像视图中都会出现黑色图像....

以下是我的代码: -

// CAMERA INTENT

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mImageCaptureUri);

startActivityForResult(i, request);

从相机意图中获取数据

Bundle extra = data.getExtras();
Bitmap photos =(Bitmap)extra.get("data");
String path=getOriginalImagePath();
File f = new File(path);  //
 ImageURI = Uri.fromFile(f);
showpotos.setImageURI(ImageURI);



public String getOriginalImagePath() {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = Profile_details.this.managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, null, null, null);
        int column_index_data = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToLast();

        return cursor.getString(column_index_data);
    }

如何解决这个问题....请帮助

4 个答案:

答案 0 :(得分:2)

删除第二个代码清单中的所有内容。

onActivityResult()中,从mImageCaptureUri加载图片。这是你告诉相机应用程序保存图像的地方。

答案 1 :(得分:1)

你好使用下面的代码: -

意图打开相机: -

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Declare mUri as globel varibale in class
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "pic_"+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(intent, 1);

ActivtyResult函数中的代码: -

if(requestCode==1) {

    image_str =getRealPathFromURI(mUri).toString();
     try {
    Picasso.with(getApplicationContext()).load(mUri)into(Imageview1);
            } catch (Exception e) {
                e.printStackTrace();
                            }   
                    }

获得真实的图像路径的功能: -

public String getRealPathFromURI(Uri contentUri)
            {
                try
                {
                    String[] proj = {MediaStore.Images.Media.DATA};
                    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    System.out.println("column_index of selected image is:"+column_index);
                    cursor.moveToFirst();
                    System.out.println("selected image path is:"+cursor.getString(column_index));
                    return cursor.getString(column_index);
                }
                catch (Exception e)
                {
                    return contentUri.getPath();
                }
            }

并在清单文件中声明以下权限: -

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

答案 2 :(得分:0)

以下是我使用的内容并且有效。如果您仍在寻找解决方案,可以尝试一下。

public void takePicture(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = Uri.fromFile(getOutputMediaFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, file);

    startActivityForResult(intent, 100);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            imageView.setImageURI(file);
    }
  }
}

答案 3 :(得分:0)

尝试此解决方案

将这些权限放入清单

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

意图捕捉图像

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }

获取捕获的图像

@Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    Bundle extras = data.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");

                    Imview.setImageBitmap(imageBitmap);

                    // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
                    Uri tempUri = getImageUri(getApplicationContext(), imageBitmap);

                    //DO SOMETHING WITH URI

                }
            }

获取图像URI的方法

public Uri getImageUri(Context inContext, Bitmap inImage) {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
                return Uri.parse(path);
}