0

我正在开发一个将绘制的画布作为 jpeg 图像存储在 SD 卡中的应用程序。问题是当我尝试查看保存的图像时,它比其他图像加载了很多时间我希望像其他图像一样在正常时间查看保存的图像我保存图像的代码是:

            View content = drawView;
            content.setDrawingCacheEnabled(true);
            content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
            Bitmap bitmap = content.getDrawingCache();

            String path = Environment.getExternalStorageDirectory().getAbsolutePath();
            String file_name="Imatge"+System.currentTimeMillis()+".jpg";
            File file = new File(path,file_name);
            FileOutputStream ostream;
            try {                   
                ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.PNG,50, ostream);
                ostream.flush();
                ostream.close();
                Toast.makeText(getApplicationContext(), " :) Image saved in "+ path+"/"+file_name, 5000).show();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.toString()+"error", 5000).show();
            }
                }

提前谢谢请帮帮我!!!!

4

3 回答 3

0

您的代码将图像保存为 PNG 文件(即使您为其提供了文件扩展名 .jpg)。PNG 文件是无损的,因此比 JPEG 文件大得多。

要修复它,请将其另存为 JPEG:

ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 50, ostream);
于 2012-05-28T05:49:13.247 回答
0

如下所示使用

        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                     FileOutputStream out = null;
                     Log.e("h",""+mview.getMeasuredHeight()+" "+mview.getMeasuredWidth());
                    try {
                         out = new FileOutputStream(new File(extStorageDirectory + "/myAwesomeDrawing.jpg"));
                         mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
                         out.flush(); 
                         out.close();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }  

其中mBitmap是您在Drawview类中使用过的 Gloabal 变量,因此通过使用此您必须跳过此行

  /*View content = drawView;
     content.setDrawingCacheEnabled(true);
        content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        Bitmap bitmap = content.getDrawingCache();
*/

可以吗

于 2012-05-28T11:13:44.263 回答
0

我自己找到了解决方案,解决方案是通过媒体存储存储图像,这是我的代码正常工作

View content = drawView;
                content.setDrawingCacheEnabled(true);
                content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
                Bitmap bitmap;
                bitmap = content.getDrawingCache();             
                String file_name="MyImage"+System.currentTimeMillis()+".jpg";

                ContentValues values = new ContentValues();
                values.put(Images.Media.TITLE,"MyImage");                   
                values.put(Images.Media.DESCRIPTION,"Advanced Practice");                   
                values.put(Images.Media.MIME_TYPE, "image/jpeg");
                Uri url = null;

                try 
                {   
                    url = getContentResolver().insert(MediaStore.Images.Thumbnails.getContentUri("external"), values);
                    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,"MyImage","Advanced Practice");                     
                    Toast.makeText(getApplicationContext(), " :) Image saved in /sdcard/DCIM/Camera/"+file_name, 5000).show();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), e.toString()+"error", 5000).show();
                }
于 2012-06-05T10:22:38.030 回答