0

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?

Community
  • 1
  • 1

1 Answers1

-1

From the Stackoverflow question Read a pdf file from assets folder

p.s. try yo search around for your answer to keep from having duplicates. :)

public class SampleActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CopyReadAssets();
    }

    private void CopyReadAssets(View v)
    {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "abc.pdf");
        try
        {
            in = assetManager.open("abc.pdf");
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e)
        {
            Log.e("tag", e.getMessage());
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/abc.pdf"),
                "application/pdf");

        startActivity(intent);
    }

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

If this does not work! Let me know! But remember, look around a bit and maybe you'll find your question already answered.

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • 1
    I know about that answer. I am not looking for code to open a PDF - I already have that. I am looking for an answer to this error when I try to run the app - Error:Execution failed for task ':app:mergeDebugAssets'. – Ali Pardawala May 24 '17 at 13:24
  • look in your debug folder and check the assets. it may be conflicting with it in their. im sure you have tried refreshing project and such, but if not do that. if that doesn't work, delete your debug folder and rebuild – Delta Enlightment May 25 '17 at 01:35