将资产文件夹复制到SD卡中会显示android中的权限被拒绝

时间:2014-02-16 16:00:18

标签: android android-permissions android-assets

朋友们,我使用此代码将我的资产文件夹复制到SD卡中,但它显示权限被拒绝但我已获得外部存储权限

复制资产

         private void CopyAssets() { 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
        files = assetManager.list(""); 
    } catch (IOException e) { 
        Log.e("tag", e.getMessage()); 
    } 
    for(String filename : files) { 
        InputStream in = null; 
        OutputStream out = null; 
        try { 
          in = assetManager.open(filename); 
          out = new FileOutputStream("/sdcard/" +"Good"); 
          copyFile(in, out); 
          in.close(); 
          in = null; 
          out.flush(); 
          out.close(); 
          out = null; 
        } catch(Exception e) { 
            Log.e("tag", e.getMessage()); 
        }        
    } 
} 
private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
    } 
}

Logcat显示权限被拒绝

                 02-16 21:25:14.227: E/tag(405): /sdcard/Good (Permission denied)
                 02-16 21:25:14.236: E/tag(405): images
                 02-16 21:25:14.236: E/tag(405): sounds
                 02-16 21:25:14.236: E/tag(405): webkit

这是我的清单文件权限标记

                 <uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

1 个答案:

答案 0 :(得分:0)

您需要使用带有getExternalStorageDirectory()

的系统SD卡可访问文件夹的getter