Environment.getExternalStorageDirectory()获取根路径的方式不友好,比如app删除,app对应的图片不删除,保存路径是sd卡根路径
替代方案
getExternalFilesDir(Environment.DIRECTORY_PICTURES) 注意:前面没有Environment,app删除对应的图片相应删除,保护隐私,保存路径是
/storage/emulated/0/Android/data/com.wintec.huashang/files/Pictures
getExternalFilesDir(null)则为:/storage/emulated/0/Android/data/com.wintec.huashang/files
实测在android7.1.2的系统上String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/a/" ;这种方法已经无法创建文件路径
public File saveBitmap(Bitmap bitmapUse) {
Log.e(“wy”, “开始保存”);
//生成一个文件,存储我们将来拍照的照片
// String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/a/" ;
String sdPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)+"/a/" ;
// String sdPath = “/mnt/sdcard/Internal Memory” ;
File file = new File(sdPath);
Log.e("wy", "绝对文件路径: " + file.getAbsoluteFile());
Log.e("wy", "文件名: " + file.getName());
if (!file.exists()) {
file.mkdirs();
Log.e("wy", "创建文件夹,路径:" + file.getPath());
}
String mPath = System.currentTimeMillis() + ".jpg";
Log.e("wy", "保存路径: " + mPath);
File f = new File(sdPath, mPath);
// File f = new File("/sdcard/namecard/", picName);
// if (f.exists()) {
// f.delete();
// }
try {
FileOutputStream out = new FileOutputStream(f);
// 10M
// bitmapUse.compress(Bitmap.CompressFormat.PNG, 90, out);
// 0.5M
bitmapUse.compress(Bitmap.CompressFormat.JPEG, 45, out);
out.flush();
out.close();
Log.e(“wy”, “已经保存”);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}