保存到内部存储大型android位图

时间:2016-03-23 08:55:21

标签: android bitmap

  private String saveToInternalStorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    fileName="profile_1";
    Date d1 = new Date();
    Random r = new Random();
    int i1 = r.nextInt(1000-1) + 1;
    fileName=  "profile-"+i1+"-"+d1.getTime();
    final File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    File mypath=new File(directory,""+fileName);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}

它的工作,但问题在于,一旦我从相机拍摄照片需要花费很多时间。在返回活动之前,它给了我黑屏。我该如何解决?

4 个答案:

答案 0 :(得分:1)

在不同的线程中执行长时间运行的任务。阅读thisthis official manual

同时查看this manual,它专门用于Bitmap处理

答案 1 :(得分:0)

尝试在asynctask中执行此操作。这样可以立即返回您的活动。

答案 2 :(得分:0)

试试这个

  private void SaveImage(Bitmap finalBitmap) {

           String root = Environment.getExternalStorageDirectory().toString();
           File myDir = new File(root + "/SaveImage");    
           myDir.mkdirs();
           Random generator = new Random();
           int n = 10000;
           n = generator.nextInt(n);
           String fname = "Image-"+ n +".jpg";
           File file = new File (myDir, fname);
           if (file.exists ()) file.delete (); 
           try {
               FileOutputStream out = new FileOutputStream(file);
               finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
               out.flush();
               out.close();

           } catch (Exception e) {
               e.printStackTrace();
           }
        }

答案 3 :(得分:0)

我遇到了同样的问题,实际上自定义相机使用的最佳分辨率无法进入预览模式。 降低分辨率, 像小米Note 4G有3600 * 4400的最佳分辨率,但是如果我在这拍照需要很多时间,所以我把它降到1280 * 720.

黑屏问题: 将参数设置到相机,然后开始预览,然后开始预览显示。

例如

camera.setparameters(params);
camera.startPreview();
camera.setPreviewDisplay(surfaceHolder);