当前位置: 代码迷 >> Android >> android sdcard资料存储 + 媒体库更新方法
  详细解决方案

android sdcard资料存储 + 媒体库更新方法

热度:16   发布时间:2016-05-01 14:01:32.0
android sdcard文件存储 + 媒体库更新方法
图片存储

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

  相关解决方案