【问题标题】:Copy pdf file from asset to sd card将 pdf 文件从资产复制到 SD 卡
【发布时间】:2013-02-24 07:46:23
【问题描述】:

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

公共类 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);
        }
    }

@覆盖 受保护的无效doInBackground(无效...参数){ // TODO 自动生成的方法存根 复制资产(); 返回空值; }

}

}

【问题讨论】:

  • 您是否在清单文件&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;中添加了权限
  • 我也有同样的问题..如果我不使用异步任务,它就可以正常工作..

标签: android


【解决方案1】:

试试这个代码

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;
    }

【讨论】:

  • 请问文件中的“文件”是什么=assetManager.list("Files");并且在 =assetManager.open("Files/"+filename);和 out = new FileOutputStream(exportDirectory+filename);
  • 它是assets文件夹中的文件名。如assets->Files
  • 我已经用你的解决方案更新了我的问题..如果有错误请纠正我
  • 将 Lecture.pdf 放入 assets 内的 Files 文件夹中,并将代码更新为 files = assetManager.list("Files");
  • 并在 =assetManager.open("/mnt/sdcard/mylecture/"+filename); 中编辑这一行To in = assetsManager.open("Files/"+filename);
【解决方案2】:

要将数据写入外部存储,我们必须在 AndroidManifest.xml 中授予权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

现在你更新了 logcat 你可以使用 AsyncTask 或 Handler 解决这个错误。

【讨论】:

  • 在这里也发布 logcat。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多