Android 是否要将FrameLayout保存在Gallery/SD卡上?它包含图像视图?
您无法将视图保存到SD卡或任何I/O流中,因为它们不可序列化和打包。但您可以将ImageView的图像另存为Android 是否要将FrameLayout保存在Gallery/SD卡上?它包含图像视图?,android,android-layout,android-imageview,android-ui,android-framework,Android,Android Layout,Android Imageview,Android Ui,Android Framework,您无法将视图保存到SD卡或任何I/O流中,因为它们不可序列化和打包。但您可以将ImageView的图像另存为 @Override public void onClick(View arg0) { // TODO Auto-generated method stub frm=(FrameLayout)findViewById(R.id.frame); frm.setDrawingCacheEna
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
frm=(FrameLayout)findViewById(R.id.frame);
frm.setDrawingCacheEnabled(true);
frm.buildDrawingCache();
bitmap = frm.getDrawingCache();
try {
File rootFile=new File(Environment.getExternalStorageDirectory()+File.separator+"MYBOARD"+File.separator);
rootFile.mkdirs();
FileOutputStream fileOutputStream = new FileOutputStream(rootFile);
bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
}
}
});
若你们想在点击一个按钮时将整个框架布局背景保存为SD卡中的图像,请在按钮的OnClickListener中写下下面的代码
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
try {
FileOutputStream fileOutputStream = new FileOutputStream("your path to save file");
bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
}
我试过这样做,现在效果很好
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame);
frameLayout.setDrawingCacheEnabled(true);
frameLayout.buildDrawingCache();
Bitmap bitmap = frameLayout.getDrawingCache();
try {
FileOutputStream fileOutputStream = new FileOutputStream("your path to save file");
bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
}
我需要检查这个代码,你测试过这个代码吗。实际上,如果我单击SaveButton(onclick),我有一个多帧布局。帧布局必须保存到gallery/sdi used FrameLayout android:id=“@+id/frame”,在我上面的代码中。我要保存在gallery@Gopal Raoyour根文件中的完整“frame”表示一个目录路径。如果我这样做的话,你应该传递一个表示类似Environment.getExternalStorageDirectory()+File.separator+“MOBOARD”+File.separator+“image.png”的文件的路径。它创建了一个image.png作为一个文件/文件夹,而不是在我的@Gopal Rao sirlet目录下的图像格式。我做了一些更改,现在就得到了,
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame);
frameLayout.setDrawingCacheEnabled(true);
frameLayout.buildDrawingCache();
Bitmap bitmap = frameLayout.getDrawingCache();
try {
FileOutputStream fileOutputStream = new FileOutputStream("your path to save file");
bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
}
try {
File rootFile=new File(Environment.getExternalStorageDirectory().toString()+"/MYBOARD");
rootFile.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".png";
resultingfile=new File(rootFile, fname);
if (resultingfile.exists ()) resultingfile.delete ();
try {
FileOutputStream Fout = new FileOutputStream(resultingfile);
bitmap.compress(CompressFormat.PNG, 100, Fout);
Fout.flush();
Fout.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
}