首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >/Photo保存图像不起作用

/Photo保存图像不起作用
EN

Stack Overflow用户
提问于 2015-10-14 15:21:49
回答 1查看 359关注 0票数 1

在我的应用程序中,用户可以选择一个图像,这是我保存在内部内存。

如果我选择内存中的图像,我的代码工作得很好,但是如果我选择云端(驱动器或照片)上的图像,它就不能工作,而且它保存了0位图像。

代码在一个片段中。

这是我代码的一部分:

首先,我选择了

代码语言:javascript
复制
                        Intent i = new Intent(
                                Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        startActivityForResult(i, 1);

然后,我用

代码语言:javascript
复制
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        Bitmap imageSelect = BitmapFactory.decodeFile(picturePath);
        cursor.close();
        ContextWrapper cw = new ContextWrapper(getContext());
        File directory = cw.getDir("images",Context.MODE_PRIVATE);
        File myPath = new File(directory,"defaultwallpaper.jpg");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(myPath);
            imageSelect.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if (fos!=null){
                    fos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }

我有这样的错误:

代码语言:javascript
复制
E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-29 21:12:04

我通过修改代码来解决这个问题,它可以在我的Nexus 5上使用Android 6:

代码语言:javascript
复制
Uri uri = data.getData();
/*String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
imageSelect = BitmapFactory.decodeFile(picturePath);
cursor.close();*/
try {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33129350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档