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; }
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
– Naveen