更改图像保存路径

问题描述:

我正在保存图像代码...它具有框架布局和覆盖图像..它工作得很好,但它保存在根文件夹中,我想将其保存在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(); 
} 

请帮助我。

尝试以下,

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(); 
     } 
} 
+0

谢谢...它就像一个神奇的... – Numair

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

+0

我已经试过** “/ DCIM /签名/” **,但它不工作 – Numair