gpt4 book ai didi

android - 将 pdf 文件从 Assets 复制到 SD 卡

转载 作者:行者123 更新时间:2023-11-29 18:02:41 25 4
gpt4 key购买 nike

在我的 Assets 文件夹中,我有一个名为 lecture.pdf 的 pdf 文件。我正在尝试将此文件从 Assets 复制到 SD 卡,以便从我的应用程序读取 pdf,但该文件无法写入 SD 卡。有人可以告诉我我做错了什么。下面是写入sd卡目录的代码

公共(public)类 XAclass 扩展 Activity {

WebView web;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.large);

new Asyntasking();

Intent intent = new Intent(getParent(), MyPdfViewerActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
"mnt/sdcard/mylecture/lecture.pdf");

startActivity(intent);


}



private class Asyntasking extends AsyncTask<Void,Void,Void>
{
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = {};
try {
files = assetManager.list("lecture.pdf");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}

for(String filename : files) {
System.out.println("File name => "+filename);
InputStream in= null;

OutputStream out = null;
try {
in = assetManager.open("/mnt/sdcard/mylecture/"+filename); // if files resides inside the "Files" directory itself
out = new FileOutputStream("/mnt/sdcard/mylecture/"+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);
}
}

@覆盖 protected Void doInBackground(Void... params) { //TODO 自动生成的方法 stub 复制 Assets (); 返回空值;

}

最佳答案

试试这段代码

private class Asyntasking extends AsyncTask<Void,Void,Void>
{
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = {};
try {
files = assetManager.list("Files");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}

for(String filename : files) {
System.out.println("File name => "+filename);
InputStream in= null;

OutputStream out = null;
try {
in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
out = new FileOutputStream(exportDirectory+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);
}
}





@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
CopyAssets();
return null;
}

关于android - 将 pdf 文件从 Assets 复制到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15049728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com