setimagebitmap在api 23 android java中不起作用

pzfprimi  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(298)

我试图上传照片从画廊到我的应用程序与位图,在我的模拟器(api 22)它的工作,但由于某种原因,在我的手机(api 23)它不工作的图片不会显示只是一个空页。这是我的密码,如果有关系的话:

public class Photoactivity extends Activity {

private static final int SELECTED_PICTURE=1;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photoactivity);

    iv=(ImageView)findViewById(R.id.ImgView);
}
public void btnClick(View v){
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECTED_PICTURE);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case SELECTED_PICTURE:
            if(resultCode==RESULT_OK){
                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));

            }
            break;

        default:
            break;
    }

}

}

7gs2gvoe

7gs2gvoe1#

image.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

设置上述 layerType 在挣扎了一周后完成了魔术。

57hvy0tb

57hvy0tb2#

替换此项:

case SELECTED_PICTURE:
        if(resultCode==RESULT_OK){
            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));

        }
        break;

有了这个:

case SELECTED_PICTURE:
        if(resultCode==RESULT_OK){
            InputStream is=getContentResolver().openInputStream(data.getData());
            imageView.setImageBitmap(BitmapFactory.decodeStream(is));
        }
        break;

更好的是,切换到使用图像加载库,比如picasso,这样i/o就可以在后台线程上完成,这样在加载图像时就不会冻结ui。

yk9xbfzb

yk9xbfzb3#

它确实有用。但不是以你的方式。在onactivityresult中检索图像时,使用uri查找其文件路径,然后对文件进行解码。问题是需要读取外部存储权限,而在api23中,您需要请求该权限。但你不必!您所需要做的就是通过解析流直接从uri获取位图。工作代码示例(也在api23上):

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
imageView.setImageBitmap(bitmap);

这样你就不会;我不需要访问文件。所以你不知道;读取外部存储器不需要权限。

相关问题