android sdcard文件存储 + 媒体库更新方法

本文探讨了Android应用中图片存储策略及多媒体库管理方法,包括如何将图片保存至SD卡、通过Intent进行媒体库全扫描与特定文件扫描,以及在不同场景下实现高效的数据存储与检索。
图片存储

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{

if(image != null)
{
String sdardDir = Environment.getExternalStorageDirectory().toString();
String name = sdardDir+"/appname/images/"+imagename+".jpg";
java.io.File file = new java.io.File(sdardDir+"/appname/images");
boolean bool = true;
if(!file.isDirectory())
{
bool = file.mkdirs() ;
}

if(bool)

{

File iamgeFile = new File(path);
if(!iamgeFile.exists())
{
try {iamgeFile.createNewFile();} catch (IOException e1) {e1.printStackTrace();error= "1"+e1.toString(); }
}
FileOutputStream fOut = null;
try { fOut = new FileOutputStream(iamgeFile); } catch (FileNotFoundException e) { e.printStackTrace();
error= "2"+e.toString(); }
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // png格式图片 image.compress(Bitmap.CompressFormat.png, 100, fOut);
try { fOut.flush(); } catch (IOException e) { e.printStackTrace();
error="3"+ e.toString(); }
try { fOut.close();} catch (IOException e) { e.printStackTrace();
error= "4"+e.toString(); }

}

}

else{

Toast t = Toast.makeText(this, "无sdcard", Toast.LENGTH_SHORT);
t.show();

}



媒体库更新

- 通过 Intent.ACTION_MEDIA_MOUNTED 进行全扫描
public void allScan(){
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}

- 通过 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 扫描某个文件
public void fileScan(String fName){
Uri data = Uri.parse("file:///"+fName);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
}



详细http://wenku.baidu.com/view/537b9b8ecc22bcd126ff0c97.html

http://devbbs.doit.com.cn/viewthread.php?tid=31528
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值