【问题标题】:SetImageBitMap isnt working in api 23 android javaSetImageBitMap 在 api 23 android java 中不起作用
【发布时间】:2016-09-04 14:59:33
【问题描述】:

我正在尝试使用位图将照片从图库上传到我的应用程序, 在我的模拟器(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;
    }

}

}

【问题讨论】:

    标签: java android image upload


    【解决方案1】:
     image.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
    

    设置上面的layerType 在经过 1 周的挣扎后发挥了作用。

    【讨论】:

      【解决方案2】:

      确实有效。但不是你这样做的方式。 当您在 onActivityResult 中检索图像时,您使用 uri 找到它的文件路径,然后解码文件。问题是需要在 api23 中请求读取外部存储权限。但你不必! 您需要做的就是通过解析流直接从 uri 获取 Bitmap。 工作代码示例(同样在 api23 上):

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

      这样您就不需要访问文件了。所以你不需要读取外部存储的权限。

      【讨论】:

        【解决方案3】:

        替换这个:

            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;
        

        更好的是,切换到使用an image-loading library,例如Picasso,这样这个I/O就可以在后台线程上完成,这样你的UI就不会在图像加载时冻结。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-22
          • 1970-01-01
          • 2014-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多