(一看就懂)Environment.getExternalStorageDirectory() is deprecated

编程入门 行业动态 更新时间:2024-10-28 15:33:06

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
// bitmapUsepress(Bitmap.CompressFormat.PNG, 90, out);
// 0.5M
bitmapUsepress(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;
}

更多推荐

(一看就懂)Environment.getExternalStorageDirectory() is deprecated

本文发布于:2023-06-13 16:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1384151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Environment   getExternalStorageDirectory   deprecated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!