在程序中,我使用以下代碼復制了asset文件夾中的所有的內容,自己創建了文件夾。
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
wallpaperDirectory.mkdirs();
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/Wallpaper/" + filename);
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);
}
}
我想獲得這個文件夾的URL地址,例如:/sdcard/Wallpaper/。我創建了文件夾,但是沒有得到文件夾的URL地址。大家看看問題出在哪裡?
在SDcard中獲取指定文件夾的URL
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
Uri.fromFile(new File(sdcard+"/Wallpaper/"));
或者:
Uri.parse(sdcard+"/Wallpaper/")