在我的应用中,我需要将一些mp4文件下载到存储中。有一个选项菜单可以选择mp4文件的位置(手机存储或SD卡存储)。 我通过反映下面的android SDK.as来获取手机存储路径和SD卡存储路径:
operator new(size_t size)
我的测试代码如下:
public class StorageUtils {
private static final String TAG = "===StorageUtil===";
public static String getPrimaryStoragePath(Context context) {
try {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
// first element in paths[] is primary storage path
return paths[0];
} catch (Exception e) {
Log.e(TAG, "getPrimaryStoragePath() failed", e);
}
return null;
}
public static String getSecondaryStoragePath(Context context) {
try {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
// second element in paths[] is secondary storage path
return paths[1];
} catch (Exception e) {
Log.e(TAG, "getSecondaryStoragePath() failed", e);
}
return null;
}
public static String getStorageState(Context context, String path) {
try {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[]{String.class});
String state = (String) getVolumeStateMethod.invoke(sm, path);
return state;
} catch (Exception e) {
Log.e(TAG, "getStorageState() failed", e);
}
return null;
}
}
然后错误:
01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err: java.io.IOException:open failed:EACCES(权限被拒绝)01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err:
在java.io.File.createNewFile(File.java:978)01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err:at com.example.fjz.demo.StorageTestActivity $ 4.run(StorageTestActivity.java:108) 01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err: 在java.lang.Thread.run(Thread.java:841)01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err:引起: libcore.io.ErrnoException:打开失败:EACCES(权限被拒绝) 01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err: 在libcore.io.Posix.open(Native Method)01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err:at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err:at java.io.File.createNewFile(File.java:971)01-18 16:05:12.819 30125-30601 / com.example.myapplication W / System.err:... 2 more
在清单文件中,我已声明了权限:
帮助我,非常感谢
答案 0 :(得分:0)
您需要在AndroidManifest.xml中添加写入权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
我声明了以下权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
幸运的是,我明白了。
1)我将sdcard
路径设为/storaget/sdcard1
2)然后我制作路径/storaget/sdcard1/Android/data/com.example.sdcard.demo
3)尝试mkdirs(path)
,然后返回false,因此我无权创建'Android/data/com.example.sdcard.demo'
目录。
4)我可以调用可以创建此目录成功的api:getExternalCacheDir
。