0

我正在实现一个保存功能以将自定义可绘制视图保存为图像。问题是创建了 JPG 图像,但它不包含任何内容,最后程序“不幸停止工作”谁能帮助我,这是我的代码;

    Bitmap b = drawView.getDrawingCache();
    File storage =Environment.getExternalStorageDirectory();
File file = new File(storage,spinner.getSelectedItem().toString()+".jpg");
    FileOutputStream fos;
              try {
               fos = new FileOutputStream(file);
               b.compress(CompressFormat.JPEG, 95,fos);
               fos.flush();
               fos.close();
              } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                drawView.destroyDrawingCache();
4

1 回答 1

0

用这个

  private void SaveIamge(Bitmap finalBitmap) {

      String root = Environment.getExternalStorageDirectory().toString();
      File myDir = new File(root + "/saved_images");    
      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();
     }
 }

并将其添加到清单中

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
于 2013-10-08T04:30:12.077 回答