1
votes

In my asset folder i have a pdf file called lecture.pdf. I am trying to copy this file from assets to sd card in order to read the pdf from my application but the file fails to write on sd card. can someone tell me what i am doing wrong. Here is the code to write to sd card directory

public class XAclass extends 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 Auto-generated method stub CopyAssets(); return null; }

}

}

2
have you added permission in manifest file <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>Naveen
same problem with me ..if i dont use async task than it work fine..Swap-IOS-Android

2 Answers

3
votes

try this code

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
votes

To write the data in external storage we will have to give the permission in AndroidManifest.xml.

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

As now you updated the logcat You can solve this error using AsyncTask or Handler.