库图像文件存在,但位图解码文件为空 [英] Gallery Image File Exists but Bitmap decode file is null

查看:87
本文介绍了库图像文件存在,但位图解码文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to show an image picked from the gallery using the uri path returned by OnActivityResult.The file exists and it is either a .png or .jpg format but Bitmap.decodeFile() is always null or for some pictures(facebook messenger saved) it's showin an Access denied error. What am I doing wrong?

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

I did read all of the related topics.

我不想使用正在工作的openStream方法或也正在工作的imageView.setImageURI选项.

I don't want to use the openStream method which was working, or the imageView.setImageURI option which was also working.

清单:

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

MainActivity onCreate:

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:

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

推荐答案

我做错了什么?

What am I doing wrong?

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

First, Android is case-sensitive, so your <uses-permission> elements are wrong. Replace ANDROID.PERMISSION with android.permission.

第二,不要求您的getRealPathFromURI()方法在所有设备上的所有图像均适用.替换:

Second, there is no requirement for your getRealPathFromURI() method to work on all devices for all images. Replace:

            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)或将其移至后台线程.

Eventually, use an image-loading library (e.g., Picasso, Glide) or otherwise move that work to a background thread.

这篇关于库图像文件存在,但位图解码文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆