I'm trying to make an app in Android Studio that opens a PDF file saved in the assets folder of the app when I tap a button. It opens the PDF through a 3rd-party app via intents.
I'm getting this error when I try to run it:
Error:Execution failed for task ':app:mergeDebugAssets'.
java.lang.NullPointerException (no error message)
My MainActivity.java code is:
public class MainActivity extends AppCompatActivity {
public void showPDF(View view) {
Toast.makeText(MainActivity.this, "button pressed", Toast.LENGTH_LONG).show();
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("pdfurl.pdf")));
AssetManager assetManager = getAssets();
File file = new File(getFilesDir(), "myfile.pdf");
try {
InputStream in = assetManager.open("myfile.pdf");
OutputStream out = openFileOutput(file.getName(), 1);
copyFile(in, out);
in.close();
out.flush();
out.close();
} catch (Exception e) {
}
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "/myfile.pdf"), "application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
while (true) {
int read = in.read(buffer);
if (read != -1) {
out.write(buffer, 0, read);
} else {
return;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
what do?