我的想法是将res / raw文件夹的内容放到名为“myFolder”的外部sd文件夹中。
我的方式是我使用Uri访问res / raw中的视频文件并使用FileOutputStream并且它无法正常工作,我的方式可行吗?
*我已经具有READ和WRITE到外部sd所需的权限,并且它在清单中被放置为correclty。
代码如下,谢谢
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
String basepath = extStorageDirectory + "/myFolder/";
Uri UriPath=Uri.parse
("android.resource://com.example.videoplayer.videoplayer/" + R.raw.video1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
File videodir= new File(basepath);
if (!videodir.exists()) {
videodir.mkdirs();
Toast.makeText(getApplicationContext(),
"Directory created!", Toast.LENGTH_LONG)
.show();
}else{
Toast.makeText(getApplicationContext(),
"Directory exists!", Toast.LENGTH_LONG)
.show();
}
File file = new File (basepath, "android.resource://com.example.videoplayer.videoplayer/" + R.raw.video1); //Im not sure if the code reaches the video1 file
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file); //this line doesnt work
Toast.makeText(getApplicationContext(),
"File created!", Toast.LENGTH_LONG)
.show();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"File error!", Toast.LENGTH_LONG)
.show();
}
}
答案 0 :(得分:0)
private void copyFileOrDir(String path) {
AssetManager assetManager = this.getAssets();
String assets[] = null;
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyFile(path);
} else {
String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
for (int i = 0; i < assets.length; ++i) {
copyFileOrDir(path + "/" + assets[i]);
}
}
} catch (IOException ex) {
Log.e("tag", "I/O Exception", ex);
}
}
private void copyFile(String filename) {
AssetManager assetManager = this.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
使用上面的代码调用函数与copyFileOrDir(&#34; myFolder&#34;);