我正在努力保存图片代码......它有框架布局和覆盖图像..它工作得非常好,但它保存在根文件夹中,我想将其保存在 sdcard / my_photos < / strong>,这是我的代码:
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 :(得分:0)
文件文件=新文件(Environment.getExternalStorageDirectory() + File.separator +“/ ghost”+ String.valueOf(roll)+“。png”); 这是问题所在。 确保您要发送到“新文件(..)”的内容是您要存储照片的路径。
答案 1 :(得分: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();
}
}