我有截屏按钮,当我拍摄截图时,图像应该存储在SD卡中,当我去看画廊时,他们会显示图像。
但是,当我一次截取屏幕截图时,我希望在图像查看器应用程序中显示。
我的代码:
public class MainActivity extends Activity {
Button captureImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
captureImage = (Button)findViewById(R.id.captureImage);
captureImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
private void saveBitmap(Bitmap bitmap) {
// TODO Auto-generated method stub
File imagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/"+"MyProfile.JPEG");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(imagePath)));
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(), "Screen short saved", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "something wrong", Toast.LENGTH_SHORT).show();
}
}
private Bitmap takeScreenshot() {
// TODO Auto-generated method stub
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
});
}
清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
截图:
我需要像这样放