android重启时间变成1970,Android 图片保存到相册时间显示为1970的问题,

该博客详细介绍了在Android应用中如何获取读写权限并实现将Base64编码的图片保存到相册的步骤,包括检查权限、请求权限、保存图片到指定文件以及更新系统图库的方法。同时提供了Java和Kotlin两种语言的实现代码。
摘要由CSDN通过智能技术生成

第一步:先申请读写权限(读写权限都要申请)

String base64 = (String) msg.obj;

bmp = ImageUtils.base64ToBitmap(base64);

if (Build.VERSION.SDK_INT >= M) {

if (ContextCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager

.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager

.PERMISSION_GRANTED) {

//进行权限请求

ActivityCompat.requestPermissions(WebViewActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_BITMAP);

} else {

saveImageToGallery(bmp);

}

} else {

saveImageToGallery(bmp);

}

/**

* 图片保存相册方法

*

* @param bitmap

*/

public void saveImageToGallery(Bitmap bitmap) {

// 首先保存图片

File file = null;

String fileName = System.currentTimeMillis() + ".jpg";

File root = new File(getExternalCacheDir(), getPackageName());

if (!root.exists()) {

root.mkdir();

}

file = new File(root, fileName);

try {

FileOutputStream fos = new FileOutputStream(file);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// //其次把文件插入到系统图库

try {

String insertImage = MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), fileName, null);

File file1 = new File(getRealPathFromURI(Uri.parse(insertImage),WebViewActivity.this));

updatePhotoMedia(file1,this);

Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

//更新图库

private void updatePhotoMedia(File file ,Context context){

Intent intent = new Intent();

intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

intent.setData(Uri.fromFile(file));

context.sendBroadcast(intent);

}

//得到绝对地址

private static String getRealPathFromURI(Uri contentUri,Context context) {

String[] proj = { MediaStore.Images.Media.DATA };

Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

cursor.moveToFirst();

String fileStr = cursor.getString(column_index);

cursor.close();

return fileStr;

}

//kotlin语法

fun saveImageToGallery(bitmap: Bitmap) {

// 首先保存图片

var file: File? = null

val fileName = (System.currentTimeMillis()).toString() + ".jpg"

val root = File(externalCacheDir, packageName)

if (!root.exists()) {

root.mkdir()

}

file = File(root, fileName)

try {

val fos = FileOutputStream(file!!)

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)

fos.flush()

fos.close()

} catch (e: FileNotFoundException) {

e.printStackTrace()

} catch (e: IOException) {

e.printStackTrace()

}

//其次把文件插入到系统图库

try {

val insertImage = MediaStore.Images.Media.insertImage(this.contentResolver, file!!.absolutePath, fileName, null)

val file1 = File(getRealPathFromURI(Uri.parse(insertImage), this))

updatePhotoMedia(file1,this);

} catch (e: FileNotFoundException) {

e.printStackTrace()

}

}

//更新图库

fun updatePhotoMedia(file: File, context: Context) {

val intent = Intent()

intent.action = Intent.ACTION_MEDIA_SCANNER_SCAN_FILE

intent.data = Uri.fromFile(file)

context.sendBroadcast(intent)

}

//得到绝对地址

fun getRealPathFromURI(contentUri: Uri, context: Context): String {

val proj = arrayOf(MediaStore.Images.Media.DATA)

val cursor = context.getContentResolver().query(contentUri, proj, null, null, null)

val column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)

cursor.moveToFirst()

val fileStr = cursor.getString(column_index)

cursor.close()

return fileStr

}

okay

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值