【问题标题】:The picture is not being shown when clicked on addImage button. What is missing in the code?单击 addImage 按钮时,图片未显示。代码中缺少什么?
【发布时间】:2019-02-13 18:52:28
【问题描述】:

我正在尝试从图库中添加图片以显示在我的应用程序中。但是在我从图库中选择图像后它没有显示。它没有显示任何错误并在我的设备上成功运行。

私有静态 int RESULT_LOAD_IMAGE=1;

public void addImage(View view){ Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(意图,RESULT_LOAD_IMAGE); }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if((requestCode == RESULT_LOAD_IMAGE) && (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.memeImage);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    用 uri 试试这个 imageView.setImageUri(selectedImage )

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      相关资源
      最近更新 更多