2013-02-24 91 views
1

在我的资产文件夹中,我有一个名为lecture.pdf的pdf文件。我试图将这个文件从资产复制到SD卡,以便从我的应用程序中读取PDF,但文件无法在SD卡上写入。有人能告诉我我做错了什么。下面是编写到SD卡目录代码将pdf文件从资产复制到SD卡

公共类XAclass延伸活动{

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(虚空...... PARAMS){// TODO自动生成方法存根 CopyAssets(); 返回null; }

} 

}

+0

你在manifest文件 添加权限'<使用许可权的android:NAME = “android.permission.WRITE_EXTERNAL_STORAGE”/>' – Naveen 2013-02-24 07:48:42

+0

同样的问题我..如果我不使用异步任务比它工作得很好。 – 2013-02-26 14:19:29

回答

2

试试这个代码

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

请问什么是files = assetManager.list(“Files”)中的“files”?和in = assetManager.open(“Files /”+ filename); out = new FileOutputStream(exportDirectory + filename); – Dimitri 2013-02-24 08:01:58

+0

它是资产folder.like文件中的文件名 - >文件 – 2013-02-24 08:04:40

+0

我已更新我的问题与您的解决方案..你可以请纠正我,如果我错了 – Dimitri 2013-02-24 08:51:54

0

要写入外部存储中的数据,我们将不得不放弃在AndroidManifest.xml的权限。

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

就像现在你更新了logcat的你能解决使用的AsyncTask或处理程序此错误。

+0

是的,它有权限 – Dimitri 2013-02-24 07:50:33

+0

也在这里发布logcat。 – 2013-02-24 07:50:54

+0

logcat在我更新的问题 – Dimitri 2013-02-24 07:56:07