【问题标题】:Getting null when setting Bitmap on Android Studio在 Android Studio 上设置位图时为空
【发布时间】:2016-05-25 04:19:06
【问题描述】:

我是 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,我无法继续使用该应用程序。

这是调试截图

感谢任何帮助,谢谢

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    试试这个

    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);
    }
    

    【讨论】:

    • 在这种情况下,Cursor 保持为空:/
    • 您已经为显示文件路径举杯了。你能说出它显示了什么吗?
    • 其实我只是用它来显示文件路径是什么,但是应用程序在显示之前就崩溃了,所以没用。
    • 所以你的应用程序崩溃了?你从来没有提到过。考虑到您尝试捕获它与位图无关。崩溃日志显示什么?我再次看到两个具有相同签名的方法 showFileChooser()
    • 对不起,双重方法,我编辑了它,当我为问题选择代码时,我复制了两次。回到问题,我的应用程序是关于将图像上传到服务器,我从我的画廊中选择图像然后上传它,所以,在获取文件路径并将图像转换为位图的过程中,我看到了位图为空,不知道为什么,如果位图为空,则无法继续上传过程,因为它崩溃了。所以希望如果 null 问题得到解决,它就不会再崩溃了。
    【解决方案2】:

    请在您的 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();
        }
    

    【讨论】:

    • 嗨,我在哪里声明 imgDecodableString?我尝试作为字符串字段,但异常表示从未在相应的 try 块中抛出,并且无法对其进行测试
    【解决方案3】:

    我发现了问题,因为我的图片都在 SD 卡中,我必须添加权限:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2015-10-23
      • 2021-09-14
      • 2018-04-12
      • 2018-04-23
      • 2017-12-27
      • 1970-01-01
      相关资源
      最近更新 更多