Android11文件存储
【摘要】
因Android11谷歌禁止使用requestLegacyExternalStorage 。 故将存储方式分为两种方式来进行文件存储,代码如下:
package com.demo.demo.utils; import android.Manifest;import android.content.ContentResolv...
因Android11谷歌禁止使用requestLegacyExternalStorage 。
故将存储方式分为两种方式来进行文件存储,代码如下:
-
package com.demo.demo.utils;
-
-
import android.Manifest;
-
import android.content.ContentResolver;
-
import android.content.ContentValues;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.pm.PackageManager;
-
import android.graphics.Bitmap;
-
import android.net.Uri;
-
import android.os.Build;
-
import android.os.Environment;
-
import android.provider.MediaStore;
-
import android.text.format.DateUtils;
-
-
import java.io.File;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
import androidx.core.content.ContextCompat;
-
-
/**
-
* 创建时间:2021/4/15
-
* 功能描述:图片保存至本地
-
*/
-
public class PictureStorageUtils {
-
public static boolean isSaveImage(Context context, Bitmap bm, String account) {
-
boolean isSave;
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { //大于等于android 10
-
isSave = saveImageQ(context, bm, account);
-
} else {
-
isSave = saveImage(context, bm, name);
-
}
-
return isSave;
-
}
-
-
//2021/4/15 功能描述:转换为jpg格式保存到本地后并获取本地图片并显示(Android10及以下版本适用的方法)
-
public static boolean saveImage(Context context, Bitmap outB, String name) {
-
String imgName = MStringUtils.isNullOrEmpty(name) ? MOtherUtils.getPictureStorageTime() : name;
-
//File.separator就是文件路径
-
String fileName = Environment.getExternalStorageDirectory() + File.separator + "DCIM"
-
+ File.separator + "demo" + File.separator;
-
try {
-
File file = new File(fileName);
-
if (!file.exists()) {
-
file.mkdirs();
-
}
-
MLog.e("saveAndGetImage:" + file);
-
File filePath = new File(file + "/" + imgName + ".png");
-
MLog.e("filePath:" + filePath);
-
FileOutputStream out = new FileOutputStream(filePath); //保存到本地,格式为JPEG
-
if (outB.compress(Bitmap.CompressFormat.PNG, 100, out)) {
-
out.flush();
-
out.close();
-
}
-
MLog.e("saveAndGetImage:END");
-
//刷新图库
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//高于22版本要手动授权
-
// 检查该权限是否已经获取
-
int i = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
-
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
-
if (i != PackageManager.PERMISSION_GRANTED) {
-
// 提示用户应该去应用设置界面手动开启权限
-
} else {
-
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(filePath)));
-
}
-
} else {
-
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(filePath)));
-
}
-
return true;
-
} catch (FileNotFoundException e) {
-
MLog.e("FileNotFoundException e.toString: " + e.toString());
-
e.printStackTrace();
-
return false;
-
} catch (IOException e) {
-
MLog.e("IOException e.toString: " + e.toString());
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
//2021/4/15 功能描述:Android10及以上保存图片到相册
-
public static boolean saveImageQ(Context context, Bitmap image, String name) {
-
Long mImageTime = System.currentTimeMillis();
-
String mImageFileName = MStringUtils.isNullOrEmpty(name) ? MOtherUtils.getPictureStorageTime() : name;
-
final ContentValues values = new ContentValues();
-
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM
-
+ File.separator + "demo"); //图库中显示的文件夹名。
-
values.put(MediaStore.MediaColumns.DISPLAY_NAME, mImageFileName);
-
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
-
values.put(MediaStore.MediaColumns.DATE_ADDED, mImageTime / 1000);
-
values.put(MediaStore.MediaColumns.DATE_MODIFIED, mImageTime / 1000);
-
values.put(MediaStore.MediaColumns.DATE_EXPIRES, (mImageTime + DateUtils.DAY_IN_MILLIS) / 1000);
-
values.put(MediaStore.MediaColumns.IS_PENDING, 1);
-
-
ContentResolver resolver = context.getContentResolver();
-
final Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
-
try {
-
// 写下我们截图的实际数据
-
try (OutputStream out = resolver.openOutputStream(uri)) {
-
if (!image.compress(Bitmap.CompressFormat.PNG, 100, out)) {
-
throw new IOException("Failed to compress");
-
}
-
}
-
// 一切都很顺利、
-
values.clear();
-
values.put(MediaStore.MediaColumns.IS_PENDING, 0);
-
values.putNull(MediaStore.MediaColumns.DATE_EXPIRES);
-
resolver.update(uri, values, null, null);
-
return true;
-
} catch (IOException e) {
-
MLog.e(e.getMessage());
-
return false;
-
}
-
}
-
}
-
对ll_dialog进行截图:
-
Bitmap bm = Bitmap.createBitmap(ll_dialog.getWidth(), ll_dialog.getHeight(),
-
Bitmap.Config.ARGB_8888);
-
Canvas canvas = new Canvas(bm);
-
ll_dialog.draw(canvas);
调用
RichPictureStorageUtils.isSaveImage(Activity.this, bm,name);
-
public static final String[] STANDARD_DIRECTORIES = {
-
DIRECTORY_MUSIC,
-
DIRECTORY_PODCASTS,
-
DIRECTORY_RINGTONES,
-
DIRECTORY_ALARMS,
-
DIRECTORY_NOTIFICATIONS,
-
DIRECTORY_PICTURES,
-
DIRECTORY_MOVIES,
-
DIRECTORY_DOWNLOADS,
-
DIRECTORY_DCIM,
-
DIRECTORY_DOCUMENTS,
-
DIRECTORY_AUDIOBOOKS,
-
};
文章来源: shuaici.blog.csdn.net,作者:帅次,版权归原作者所有,如需转载,请联系作者。
原文链接:shuaici.blog.csdn.net/article/details/116942978
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)