1

i use this code to create a folder in sdcard n it is created sucesfully

private void CreateFolder() {
    File folder = new File(Environment.getExternalStorageDirectory() +
            File.separator + "Books");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        Toast.makeText(MainActivity.this, "Books Folder Created", Toast.LENGTH_SHORT).show();
    } else {
        // Do something else on failure
        Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show();


    }

}

And these lines of code to copy a file from res/raw to this folder but this cannot copy specified pdf file in that folder

 try{
                copyFile(getResources().openRawResource(R.raw.the_reader)
                , new FileOutputStream(new File(context.getFilesDir(),"/sdcard/Books/the_reader.pdf")));
                }
                catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(context,"Fail",Toast.LENGTH_LONG).show();
                }



                File pdfFile = new File(context.getFilesDir(), "/sdcard/Books/the_reader.pdf");
                Uri path = Uri.fromFile(pdfFile);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setDataAndType(path, "application/pdf");
                startActivity(intent);

Copy file code

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

But no able to copy permissions are also added in manifest

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

It cannot copy pdf file in that specified folder ...

comrade
  • 4,590
  • 5
  • 33
  • 48
shukar
  • 19
  • 2

1 Answers1

0

some changes to you copyFile method:

public void copy(InpputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) > 0) {
        out.write(buffer, 0, read);
    }
    in.close();
    out.close();
}
Konstantin
  • 174
  • 7