库图像文件存在但位图解码文件为空

时间:2017-12-12 22:59:22

标签: android bitmap

我尝试使用OnActivityResult返回的uri路径显示从图库中挑选的图像。该文件存在且为.png.jpg格式但Bitmap.decodeFile()始终为null或某些图片(facebook messenger已保存),它会显示Access denied error。我究竟做错了什么?

我确实阅读了所有相关主题。

我不想使用正在运行的openStream方法,或者也使用imageView.setImageURI选项。

清单:

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

MainActivity onCreate:

// required by Android 6.0 +
        checkPermissions(getApplicationContext());
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent gallery =
                        new Intent(Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(gallery, PICK_IMAGES);
            }
        });

MainActivity onActivitiyResult:

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (requestCode == PICK_IMAGES && resultCode == RESULT_OK && data != null) {

                String imagePath = "";
                Uri targetUri = data.getData();
                if (data.toString().contains("content:")) {
                    imagePath = getRealPathFromURI(targetUri);
                } else if (data.toString().contains("file:")) {
                    imagePath = targetUri.getPath();
                } else {
                    imagePath = null;
                }

                ImageView imageView = (ImageView) findViewById(R.id.imageView);

                Log.d("Image",imagePath);

                File thePath = new File(imagePath);

                if(thePath.exists()){
                    Log.d("Image", "exists");
                }

                Bitmap myBitmap = BitmapFactory.decodeFile(thePath.getAbsolutePath());
                if(myBitmap == null)
                Log.d("Image","bummer");
                imageView.setImageBitmap(myBitmap);

            } else {
                Log.d(TAG, "Something went wrong");
            }
        }

        public String getRealPathFromURI(Uri contentUri) {
            Cursor cursor = null;
            try {
                String[] proj = {MediaStore.Images.Media.DATA};
                cursor = getContentResolver().query(contentUri, proj, null, null,
                        null);
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

  

我做错了什么?

首先,Android区分大小写,因此您的<uses-permission>元素是错误的。将ANDROID.PERMISSION替换为android.permission

其次,您的getRealPathFromURI()方法不需要在所有设备上使用所有图像。替换:

            String imagePath = "";
            Uri targetUri = data.getData();
            if (data.toString().contains("content:")) {
                imagePath = getRealPathFromURI(targetUri);
            } else if (data.toString().contains("file:")) {
                imagePath = targetUri.getPath();
            } else {
                imagePath = null;
            }

            ImageView imageView = (ImageView) findViewById(R.id.imageView);

            Log.d("Image",imagePath);

            File thePath = new File(imagePath);

            if(thePath.exists()){
                Log.d("Image", "exists");
            }

            Bitmap myBitmap = BitmapFactory.decodeFile(thePath.getAbsolutePath());
            if(myBitmap == null)
            Log.d("Image","bummer");
            imageView.setImageBitmap(myBitmap);

使用:

            Uri targetUri = data.getData();
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            Bitmap myBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
            if(myBitmap == null)
            Log.d("Image","bummer");
            imageView.setImageBitmap(myBitmap);

最终,使用图片加载库(例如Picasso,Glide)或以其他方式将该作品移至后台主题。

答案 1 :(得分:0)

Bitmap.decodeFile() is always null 

如果位图对于可用内存而言太大,则decodeFile()返回null。就像decodeStream()一样。

尝试使用较小的图片。或者在加载时缩小。