来自照片库的Android图片

时间:2017-02-16 07:21:40

标签: android image

System.out.println("weeeeeeeee");
try {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    System.out.println("111111");
    System.out.println(Common.getIm1());
    Common.getIm1().setImageBitmap(thumbnail);
}
catch(Exception e)
{
    System.out.println("dakfls" + e.getMessage());
}

基本上是在onactivityresult函数中发生了什么,它给了我以下错误。 dakflsAttempt在空对象引用上调用虚方法'java.lang.Object android.os.Bundle.get(java.lang.String)'。在打印1111之前发生崩溃。 我需要完整的图像而不是缩略图

我从像我这样的片段开始调用

try
{
    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    getActivity().startActivityForResult(i, 100);
}
catch(Exception e)
{

}

4 个答案:

答案 0 :(得分:1)

要在ACTION_PICKgetData()正确接收Intent的数据。像这样:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        if (resultCode == RESULT_OK) {
             Uri imageUri = data.getData();
             Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
             Common.getIm1().setImageBitmap(bitmap);
        }
        break;
    }
}

注意:您应将代码100放在班级顶部的static final int

答案 1 :(得分:0)

试试此代码

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 100);

您将在onActivityResult上获得图片路径:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

答案 2 :(得分:0)

您只需要使用返回的Intent中的数据,它的内容样式为Uri,并使用ContentResolver查询或打开它

Uri dataUri = data.getData();
ContentResolver contentResolver = getContentResolver();
InputStream stream = contentResolver.openInputStream(dataUri); //if you sure this content Uri is refer a file
BitmapFactory.decodeStream(stream);

答案 3 :(得分:0)

 photoBrowseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, DataHolder.INTENT_BROWSE_GALLERY);
        }
    });

并按照您的意愿使用图像的uri如下

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == DataHolder.INTENT_BROWSE_GALLERY) {
        if (data != null) {
            Uri targetUri = data.getData();

        }
    }

}