错误:任务':app:mergeDebugAssets'的执行失败

时间:2017-05-24 12:55:39

标签: java android android-studio android-intent android-assets

我正在尝试在Android Studio中创建一个应用程序,当我点击按钮时,该应用程序会打开保存在应用程序资源文件夹中的PDF文件。它通过意图通过第三方应用打开PDF。

我尝试运行时遇到此错误:

  

错误:任务':app:mergeDebugAssets'执行失败。

     

java.lang.NullPointerException(无错误消息)

我的MainActivity.java代码是:

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

做什么?

1 个答案:

答案 0 :(得分:-1)

来自Stackoverflow问题Read a pdf file from assets folder

P.S。尝试搜索你的答案,以避免重复。 :)

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

如果这不起作用!让我知道!但请记住,环顾四周,也许你会发现你的问题已经回答了。