1

私のアセットフォルダには、lecture.pdfというPDFファイルがあります。アプリケーションからPDFを読み取るために、このファイルをアセットからSDカードにコピーしようとしていますが、ファイルを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);
        }
    }

@Override protected Void doInBackground(Void ... params){// TODO自動生成されたメソッドスタブCopyAssets(); nullを返します。}

}

}

4

2 に答える 2

3

このコードを試してください

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;
    }
于 2013-02-24T07:57:10.353 に答える
0

外部ストレージにデータを書き込むには、AndroidManifest.xmlで権限を付与する必要があります。

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

logcatを更新したので、AsyncTaskまたはHandlerを使用してこのエラーを解決できます。

于 2013-02-24T07:49:07.430 に答える