butfon= (Button)findViewById(R.id.butfon);
butfon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout linLayout = (RelativeLayout) findViewById(R.id.rl);
linLayout.buildDrawingCache();
Bitmap bitmap = linLayout.getDrawingCache();
SaveImage(bitmap);
}});
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Pictures");
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, 95, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); // регистрация в фотоальбоме
Toast.makeText(this, "Результат сохранён в галерею", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}}
-
Вопрос задан
-
98 просмотров