I have a button in my app, which when clicked on will open a PDF (the "Open with" prompt will show). The PDF file is in my assets folder of the app. I have written the code such that an AsyncTask carries out the operation of copying from the assets folder to external storage and then opening it. On running however, none of the methods of AsyncTask class execute as I can see from Logcat. I have seen other similar questions and tried their different answers:
- .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) instead of .execute()
- .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
- adding onPreExecute()
The error I get is NullPointerException in the onPostExecute(File file) method, i.e. file is not found. None of the logged statements in the three methods are in logcat.
I call this execute method below:
public void execute() {
Context context = contextWeakReference.get();
if (context != null) {
new CopyFileAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
}
}
private class CopyFileAsyncTask extends AsyncTask<Void, Void, File> {
final String appDirectoryName = BuildConfig.APPLICATION_ID;
final File fileRoot = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), appDirectoryName);
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e("AsyncTask", "onPreExecute");
}
@Override
protected File doInBackground(Void... params) {
Context context = contextWeakReference.get();
AssetManager assetManager = context.getAssets();
File file = new File(fileRoot, fileName);
InputStream in = null;
OutputStream out = null;
try {
file.mkdirs();
if (file.exists()) {
file.delete();
}
file.createNewFile();
in = assetManager.open(fileName);
Log.d(TAG, "In");
out = new FileOutputStream(file);
Log.d(TAG, "Out");
Log.d(TAG, "Copy file");
copyFile(in, out);
Log.d(TAG, "Close");
in.close();
out.flush();
out.close();
return file;
} catch (Exception e)
{
Log.e(TAG, e.getMessage());
}
return null;
}
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 onPostExecute(File file) {
super.onPostExecute(file);
Context context = contextWeakReference.get();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(file),
"application/pdf");
context.startActivity(intent);
}
}