在Android Studio上设置位图时获取null

时间:2016-05-23 03:01:18

标签: android bitmap

我是Android开发的新手,我目前正在使用Bitmap,我正在尝试从我的设备的文件路径设置位图,但每次调试时,它仍然为空,我已经搜索过类似的问题我做了一些人的建议,但它仍然没有用,有人能给我一个我做错的线索吗?

private int PICK_IMAGE_REQUEST = 1;

private ImageButton buttonChoose;
private Button buttonUpload;
private Button buttonView;

private ImageView imageView;

private Bitmap bitmap;

private Uri filePath;



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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        Toast.makeText(this,filePath.toString(),Toast.LENGTH_LONG);

        try {

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);



        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

从这一行调试时:

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

它保持Null,我无法继续使用该应用程序。

这是调试截图
enter image description here

感谢任何帮助,谢谢

3 个答案:

答案 0 :(得分:0)

试试这个

String path;
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if(cursor.moveToFIrst()){
    path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close;

    //get bitmap from path;
    // you can also check whether the file exists and can be read here(not necessary).If you do that try to use DocumentFile
    bitmap=BitmapFactory.decodeFile(path);
}

答案 1 :(得分:0)

请在onActivityResult()方法中进行以下更改,然后尝试

希望它能奏效。

   try 
   {
        // When an Image is picked
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgDecodableString = cursor.getString(columnIndex);
            cursor.close();

            // Set the Image in ImageView after decoding the String
            imageView.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

答案 2 :(得分:0)

我发现了问题,因为我的图片全部都在SD卡中,我不得不添加权限:

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

这就是我无法正确获取位图的原因。可能是一个新手错误,但我刚刚开始,感谢那些花时间帮助我的人,我真的很感激!