参考 http://blog.youkuaiyun.com/xu_fu/article/details/39158747
中间 MediaStore.Images.Media.insertImage(context.getContentResolver(), appDir.getAbsolutePath(), fileName, null); 出现NullPointException,去掉context即可。
bt_image点击实现是实现图片保存到指定的文件夹,同时又需要图片出现在图库里
本人具体实现如下:
1、添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2、布局是两个button控件,id是 bt_image和bt
3、具体代码实现如下:
public class ViewActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt,bt_image;
private Context context;
Bitmap bitmap,bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screenshot);
bt = (Button) findViewById(R.id.bt);
bt_image= (Button) findViewById(R.id.bt_image);
bt_image.setOnClickListener(this);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_image:
//截屏
View view = v.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
bmp = view.getDrawingCache();
// 首先保存图片
File appDir = new File(Environment.getExternalStorageDirectory(), "Damily");
if (!appDir.exists()) {
appDir.mkdir();
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
String fileName=sdf.format(new Date())+".jpg";
//or String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
System.out.println( file.getAbsolutePath().toString());
System.out.println( appDir.toString());
if (bmp != null) {
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(getContentResolver(),
appDir.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
// scanIntent.setData(Uri.fromFile(new File("/sdcard/Damily/"+fileName)));
scanIntent.setData(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +"/Damily/"+ fileName)));
sendBroadcast(scanIntent);
// sendNotification();下一篇博客实现
break;
//下面代码是只保存图片到指定文件夹中
/* case R.id.bt:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
String fpath="/sdcard/"+sdf.format(new Date())+".png";
View view = v.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
bitmap = view.getDrawingCache();
if (bitmap != null) {
System.out.println("got");
try {
FileOutputStream fos=new FileOutputStream(fpath);
bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);
fos.flush();
fos.close();
System.out.println("file---"+fpath+"----output done.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("bitmap is NULL");
}
break;*/
}
}
}
运行结果如下:
// sendNotification(); 截图完发送通知,并且点击通知可以查看该截图