0

我正在保存图像代码...它具有框架布局和叠加图像..它工作得很好,但它保存在根文件夹中,我想将它保存在sdcard/my_photos中,这是我的代码:

FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame);
Random fCount = new Random();
int roll = fCount.nextInt(600) + 1;                       
File file = new File(Environment.getExternalStorageDirectory()
    + File.separator + "/ghost" + String.valueOf(roll) +".png" );

Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(),
    mainLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
mainLayout.draw(c);
FileOutputStream fos = null;
try {
  fos = new FileOutputStream(file);

  if (fos != null) {
    b.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
  }
} catch (Exception e) {
  e.printStackTrace();
}

请帮帮我。

4

2 回答 2

0

文件 file = new File(Environment.getExternalStorageDirectory() + File.separator + "/ghost" + String.valueOf(roll) +".png" ); 是这里的问题。确保您发送到“新文件(..)”的内容是您要存储照片的路径。

于 2012-05-23T21:27:35.923 回答
0

试试下面的,

bool IsDirCreated=false;
File f = new File(Environment.getExternalStorageDirectory()+ File.separator+ "ghost");
if(!f.isDirectory()) 
{
   IsDirCreated=f.mkdirs();
}
else
{
   IsDirCreated=true;
}
if(IsDirCreated==true)
{
        FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame);
        Random fCount = new Random();
        int roll = fCount.nextInt(600) + 1;                       
        File file = new File(Environment.getExternalStorageDirectory()
    + File.separator + "/ghost/" + String.valueOf(roll) +".png" );

        Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(),
        mainLayout.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        mainLayout.draw(c);
        FileOutputStream fos = null;
        try 
        {
            fos = new FileOutputStream(file);
            if (fos != null) {
            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.close();
        }
        catch (Exception e) 
        {
          e.printStackTrace();
        }
}
于 2012-05-24T04:28:34.587 回答