将位图写入sdcard中的文件

时间:2011-07-09 11:55:02

标签: android bitmap

我正在尝试在我的代码中将位图压缩为jpg。我得到一个nullpointer异常,请建议

new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Cursor imageCursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Video.Media.DATA},MediaStore.Video.Media.DISPLAY_NAME+"=?" ,new String[]{imageTitle},null);
                        imageCursor.moveToFirst();
                        final String imageData=imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Video.Media.DATA));
                        dbConnection connection=new dbConnection(getApplicationContext());
                        SQLiteDatabase db=connection.getWritableDatabase();
                        File imageFile=new File(imageData);
                        File outputFile=new File("mnt/sdcard/Images/hiddenImage.jpg");
                        InputStream is;
                        try 
                        {
                            is = new FileInputStream(imageFile);
                            InputStreamReader inputreader=new InputStreamReader(is);
                            BufferedReader buffReader=new BufferedReader(inputreader);
                            final ByteArrayBuffer buffer=new ByteArrayBuffer(512);
                            int i;
                            while((i=buffReader.read())!=-1)
                            {
                                buffer.append(i);
                            }
                            Bitmap imagefile=BitmapFactory.decodeByteArray(buffer.toByteArray(), 0, buffer.toByteArray().length);
                            OutputStream os=new FileOutputStream("mnt/sdcard/Images/testing.jpg");
                            imagefile.compress(Bitmap.CompressFormat.JPEG, 100, os);
                            os.close();
                        } 
                        catch (FileNotFoundException e) 
                        {
                            e.printStackTrace();
                        } 
                        catch (IOException e) 
                        {
                            e.printStackTrace();
                        }

                    }
                }).start();

这是堆栈跟踪:

07-09 14:57:48.595: ERROR/AndroidRuntime(5761): FATAL EXCEPTION: Thread-9
07-09 14:57:48.595: ERROR/AndroidRuntime(5761): java.lang.NullPointerException
07-09 14:57:48.595: ERROR/AndroidRuntime(5761):     at com.messageHider.viewImageThumb$1$1.run(viewImageThumb.java:106)
07-09 14:57:48.595: ERROR/AndroidRuntime(5761):     at java.lang.Thread.run(Thread.java:1096)

在我尝试压缩位图的地方抛出异常: //::imagefile.compress(Bitmap.CompressFormat.JPEG,100,os);

1 个答案:

答案 0 :(得分:2)

如果NullPointerException中有imagefile.compress(Bitmap.CompressFormat.JPEG, 100, os);,则imagefile必须为null。如果BitmapFactory无法解码图像,则会发生这种情况。

此外:

  • 您的文件读取算法效率很低。请考虑一次读取多个字节。或者,更好的是,decodeStream()上使用decodeByteArray()而不是BitmapFactory

  • 确保您拥有WRITE_EXTERNAL_STORAGE权限。

  • 不要像在这里那样硬连接路径,特别是错误路径。使用Environment.getExternalStorageDirectory()获取外部存储的根目录,然后使用适当的File构造函数来组合指向该目录中所需文件的File对象。

  • 您无需确保Images/目录确实存在。

相关问题