BitmapFactory无法去code流 [英] BitmapFactory Unable to decode stream

查看:518
本文介绍了BitmapFactory无法去code流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我不知道为什么这样下去我在我的画廊选择一个图像,每次来了?

下面是code:

 如果(V == uploadImageButton){
                        //下面,您可以打开手机图库
                        意向意图=新的意图();
                        intent.setType(图像/ *);
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                        Intent.createChooser(原意,用完整的行动),1);
                }


公共无效onActivityResult(INT申请code,INT结果code,意图数据){
                如果(结果code == RESULT_OK和放大器;&安培;请求code == 1安培;&安培;!空=数据){
                        //位图的照片=(位图)data.getData()getPath()。
                        乌里selectedImageUri = data.getData();
                        的String [] filePathColumn = {MediaStore.Images.Media.DATA};

                        光标光标= getContentResolver()查询(selectedImageUri,
                                        filePathColumn,NULL,NULL,NULL);
                        cursor.moveToFirst();

                        INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
                        字符串picturePath = cursor.getString(参数:columnIndex);
                        cursor.close();
                        Log.e(图片,picturePath);
                        德codeFILE(picturePath);
                }
        }


公共无效去codeFILE(字符串文件路径){
                // TODO自动生成方法存根
                //德code图像尺寸
                BitmapFactory.Options O =新BitmapFactory.Options();
                o.inJustDe codeBounds = TRUE;
                BitmapFactory.de codeFILE(文件路径,O);

                //我们希望新的大小扩展到
                最终诠释REQUIRED_SIZE = 1024;

                //找到正确的比例值。它应该是2的幂。
                INT width_tmp = o.outWidth,height_tmp = o.outHeight;
                int标= 1;
                而(真){
                        如果(width_tmp< REQUIRED_SIZE和放大器;&安培; height_tmp< REQUIRED_SIZE){
                                打破;
                        }
                        width_tmp / = 2;
                        height_tmp / = 2;
                        规模* = 2;
                }

                //德code与inSampleSize
                BitmapFactory.Options O2 =新BitmapFactory.Options();
                o2.inSampleSize =规模;
                点阵位图= BitmapFactory.de codeFILE(文件路径,O2);

                imageview.setImageBitmap(位);
        }
 

错误:

  01-17 23:18:04.642:D /文档(25157):onFinished()[内容://com.android.providers.media.documents/document/image% 3A901]
01-17 23:18:04.682:E / BitmapFactory(24993):无法取消code流:显示java.lang.NullPointerException
01-17 23:18:04.682:E / BitmapFactory(24993):无法取消code流:显示java.lang.NullPointerException
01-17 23:18:09.732的:I / InputReader(766):重新配置输入设备。变化= 0x00000004
01-17 23:18:09.732:I / InputReader(766):设备重新配置:n = 4,NAME ='touch_dev',尺寸1080x1920,定位3,模式1,显示ID 0
 

解决方案

不要以为有一个文件路径。 Android 4.4系统和高达约将其删除。而你得到的URI已经没有路径。

您仍可以访问该文件的内容,无论通过的InputStream (ContentResolver的#openInputStream(URI URI)<$ C C $> )或通过文件描述符。

它在这里解释:<一href="https://developer.android.com/guide/topics/providers/document-provider.html#open">ContentProviders:打开一个文档(向下滚动,链接,部分似乎被打破)

这不工作在较旧的Andr​​oid版本了。

  @覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK和放大器;&安培;请求code == 1安培;&安培;!空=数据){
        德codeURI(data.getData());
    }
}

公共无效去codeURI(URI URI){
    ParcelFileDescriptor parcelFD = NULL;
    尝试 {
        parcelFD = getContentResolver()openFileDescriptor(URI,R)。
        当前FileDescriptor的ImageSource = parcelFD.getFileDescriptor();

        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeFileDescriptor(ImageSource的,空,O);

        //我们希望新的大小扩展到
        最终诠释REQUIRED_SIZE = 1024;

        //找到正确的比例值。它应该是2的幂。
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 1;
        而(真){
            如果(width_tmp&LT; REQUIRED_SIZE和放大器;&安培; height_tmp&LT; REQUIRED_SIZE){
                打破;
            }
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }

        //德code与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        点阵位图= BitmapFactory.de codeFileDescriptor(ImageSource的,空,O2);

        imageview.setImageBitmap(位);

    }赶上(FileNotFoundException异常E){
        //处理错误
    }赶上(IOException异常E){
        //处理错误
    } 最后 {
        如果(parcelFD!= NULL)
            尝试 {
                parcelFD.close();
            }赶上(IOException异常E){
                //忽略
            }
    }
}
 

Hey I am not sure why this keeps coming up every time I select an image in my gallery?

Here is the code:

if (v == uploadImageButton) {
                        // below allows you to open the phones gallery
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                        Intent.createChooser(intent, "Complete action using"), 1);
                }


public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (resultCode == RESULT_OK && requestCode == 1 && null != data) {
                        // Bitmap photo = (Bitmap) data.getData().getPath();
                        Uri selectedImageUri = data.getData();
                        String[] filePathColumn = { MediaStore.Images.Media.DATA };

                        Cursor cursor = getContentResolver().query(selectedImageUri,
                                        filePathColumn, null, null, null);
                        cursor.moveToFirst();

                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        String picturePath = cursor.getString(columnIndex);
                        cursor.close();
                        Log.e("Picture", picturePath);
                        decodeFile(picturePath);
                }
        }


public void decodeFile(String filePath) {
                // TODO Auto-generated method stub
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filePath, o);

                // the new size we want to scale to
                final int REQUIRED_SIZE = 1024;

                // Find the correct scale value. It should be the power of 2.
                int width_tmp = o.outWidth, height_tmp = o.outHeight;
                int scale = 1;
                while (true) {
                        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                                break;
                        }
                        width_tmp /= 2;
                        height_tmp /= 2;
                        scale *= 2;
                }

                // decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);

                imageview.setImageBitmap(bitmap);
        }

Error:

01-17 23:18:04.642: D/Documents(25157): onFinished() [content://com.android.providers.media.documents/document/image%3A901]
01-17 23:18:04.682: E/BitmapFactory(24993): Unable to decode stream: java.lang.NullPointerException
01-17 23:18:04.682: E/BitmapFactory(24993): Unable to decode stream: java.lang.NullPointerException
01-17 23:18:09.732: I/InputReader(766): Reconfiguring input devices.  changes=0x00000004
01-17 23:18:09.732: I/InputReader(766): Device reconfigured: id=4, name='touch_dev', size 1080x1920, orientation 3, mode 1, display id 0

解决方案

Don't assume that there is a file path. Android 4.4 and up are about to remove them. And the uri you got has already no path.

You can still access the file content either through an InputStream (ContentResolver#openInputStream(Uri uri)) or through a file descriptor.

It's explained here: ContentProviders: Open a document (scroll down, link to section seems to be broken)

And that does work on older android versions too.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == 1 && null != data) {
        decodeUri(data.getData());
    }
}

public void decodeUri(Uri uri) {
    ParcelFileDescriptor parcelFD = null;
    try {
        parcelFD = getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor imageSource = parcelFD.getFileDescriptor();

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(imageSource, null, o);

        // the new size we want to scale to
        final int REQUIRED_SIZE = 1024;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2);

        imageview.setImageBitmap(bitmap);

    } catch (FileNotFoundException e) {
        // handle errors
    } catch (IOException e) {
        // handle errors
    } finally {
        if (parcelFD != null)
            try {
                parcelFD.close();
            } catch (IOException e) {
                // ignored
            }
    }
}

这篇关于BitmapFactory无法去code流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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