我使用此代码在sdcard中创建一个文件夹,它是创建的
private void CreateFolder() {
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "Books");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
Toast.makeText(MainActivity.this, "Books Folder Created", Toast.LENGTH_SHORT).show();
} else {
// Do something else on failure
Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show();
}
}
这些代码行将文件从res / raw复制到此文件夹但这不能复制该文件夹中指定的pdf文件
try{
copyFile(getResources().openRawResource(R.raw.the_reader)
, new FileOutputStream(new File(context.getFilesDir(),"/sdcard/Books/the_reader.pdf")));
}
catch (IOException e) {
e.printStackTrace();
Toast.makeText(context,"Fail",Toast.LENGTH_LONG).show();
}
File pdfFile = new File(context.getFilesDir(), "/sdcard/Books/the_reader.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
复制文件代码
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);
}
}
但是清单
中还添加了无法复制权限的功能 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
它无法复制指定文件夹中的pdf文件...
答案 0 :(得分:0)
copyFile方法的一些更改:
public void copy(InpputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) > 0) {
out.write(buffer, 0, read);
}
in.close();
out.close();
}