android 6.0获取图片地址,Android应用开发Android 保存图片到系统相册(三星6.0有效)...

本文主要介绍在Android 6.0系统中保存图片到系统相册的方法。先给出网上常见保存图片并插入系统图库的代码,指出图片过大时会出现oom异常,随后给出解决该异常的新方法。最后提到通知图库更新的两种方式,分析其在不同系统版本的适用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android   保存图片到系统相册(三星6.0有效)。今天要做一个保存图片到系统图库的功能,自身能力较浅,所以只能搜索了但发现网上的方法有几处bug,所以自己总结一下防止以后忘掉也想和大家分享一下.首页网上保存图片并插入系统图库的方法:

//   首先保存图片

File appDir = new   File(SAMPLE_DEFAULT_DIR);

if (!appDir.exists()) {

appDir.mkdir();

}

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

File file = new File(appDir,   fileName);

try {

FileOutputStream fos = new   FileOutputStream(file);

bmp.compress(CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

} catch (FileNotFoundException e)   {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

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

try {

MediaStore.Images.Media.insertImage(context.getContentResolver(),

file.getAbsolutePath(), fileName, null);

} catch (FileNotFoundException e)   {

e.printStackTrace();

}

我之前也用这种方法保存,但是我发现图片过大时   调用MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),   fileName, null);方法会出现oom异常,很讨厌.所以我又在网上找了一个方法:

public boolean saveImageToGallery(Context context, Bitmap   bitmap) {

// 首先保存图片

File dir = new   File(SAMPLE_DEFAULT_DIR);

if (!dir.exists()) {

dir.mkdirs();

}

// 系统时间

long dateTaken =   System.currentTimeMillis();

// 图像名称

final String filename =   DateFormat.format("yyyy-MM-dd kk.mm.ss", dateTaken)

.toString() +   ".jpg";

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

try {

//              MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),   filename, null);

HexOneToOneBitmapUtil.insertImage(getActivity().getContentResolver(),   filename, dateTaken, SAMPLE_DEFAULT_DIR,filename, bitmap, null);

} catch (Exception e) {

HexLog.e(e.getMessage());

return false;

}

}

public static Uri insertImage(ContentResolver cr, String name,   long dateTaken,

String   directory, String filename, Bitmap source, byte[] jpegData) {

OutputStream outputStream =   null;

String filePath = directory +   filename;

try {

File dir = new   File(directory);

if (!dir.exists())   {

dir.mkdirs();

}

File file = new   File(directory, filename);

if (file.createNewFile())   {

outputStream = new   FileOutputStream(file);

if (source != null)   {

source.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

} else {

outputStream.write(jpegData);

}

}

} catch   (FileNotFoundException e) {

HexLog.e(e.getMessage());

return null;

} catch (IOException e)   {

HexLog.e(e.getMessage());

return null;

} finally {

if (outputStream != null)   {

try {

outputStream.close();

} catch (Throwable t)   {

}

}

}

ContentValues values = new   ContentValues(7);

values.put(MediaStore.Images.Media.TITLE, name);

values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);

values.put(MediaStore.Images.Media.DATE_TAKEN, dateTaken);

values.put(MediaStore.Images.Media.MIME_TYPE,   "image/jpeg");

values.put(MediaStore.Images.Media.DATA, filePath);

return cr.insert(IMAGE_URI,   values);

}

这样就可以完美解决oom异常了.

虽然咱们把图片保存到本地了,但是咱们得发通知从新检索一遍,才能在图库中看到咱们的图片.通常有两种

// 最后通知图库更新

try {

context.sendBroadcast(new   Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new   File(file.getPath()))));

}catch (Exception e){

return false;

}

final MediaScannerConnection msc = new MediaScannerConnection(mContext,   new MediaScannerConnectionClient() {

public void   onMediaScannerConnected() {

msc.scanFile(SAMPLE_DEFAULT_DIR,filename);

}

public void onScanCompleted(String   path, Uri uri) {

Log.v(TAG, "scan   completed");

msc.disconnect();

}

});

网上说第一种发广播的方法在4.4以后就会出现异常,因为4.4以后只有系统软件才可以有扫描SD卡的权限,咱们的app肯定得适配不同的操作系统吧,所以只能用第二种方法了.但是把第二种方法拷到代码中会报错,根本不能这样用.所以咱们可以这样用

private MediaScannerConnection mMediaonnection;

// 最后通知图库更新

try {

mMediaonnection = new   MediaScannerConnection(context, new   MediaScannerConnection.MediaScannerConnectionClient() {body{ }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值