图片存储

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐