[英]To take screenshot and save it into sd card
在这里,我尝试拍摄屏幕快照,然后保存到SD卡中,但是保存时失败了。 请帮我。
public void onClick(View OnclickView) {
boolean success = false;
view = OnclickView.getRootView();
view.setDrawingCacheEnabled(true);
bitmap = view.getDrawingCache();
ScreenShotHold.setImageBitmap(bitmap);
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "test.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
Toast.makeText(getApplicationContext(), "bitmap " + bitmap, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "outstream " + outStream, Toast.LENGTH_SHORT).show();
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success == true) {
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
}
在这里,我得到的结果是失败的,但是正在获得屏幕截图。
你可以试试这个
Bitmap screenshotedBitmap = yourBitmap;
String lastInsertedPath = MediaStore.Images.Media.insertImage(getContentResolver(), screenshotedBitmap, "myImage", "modified image");
This works perfect, I got my answer.
view = OnclickView.getRootView();
view.setDrawingCacheEnabled(true);
bitmap = view.getDrawingCache();
count++;
Toast.makeText(MainActivity.this, "count is "+count, Toast.LENGTH_LONG).show();
imageview.setImageBitmap(bitmap);
if(count==1)
{
bitmap = ((BitmapDrawable)imageview.getDrawable()).getBitmap();
ImagePath = MediaStore.Images.Media.insertImage(
getContentResolver(),
bitmap,
"demo_image",
"demo_image"
);
URI = Uri.parse(ImagePath);
Toast.makeText(MainActivity.this, "Image Saved Successfully", Toast.LENGTH_LONG).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.