【问题标题】:Error:Execution failed for task ':app:mergeDebugAssets'错误:任务“:app:mergeDebugAssets”执行失败
【发布时间】:2017-10-24 20:06:18
【问题描述】:

我正在尝试在 Android Studio 中创建一个应用程序,当我点击一个按钮时,它会打开一个保存在应用程序资产文件夹中的 PDF 文件。它通过意图通过 3rd 方应用程序打开 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);
}
}

做什么?

【问题讨论】:

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


    【解决方案1】:

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

    附言尝试四处搜索您的答案,以免重复。 :)

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

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

    【讨论】:

    • 我知道那个答案。我不是在寻找打开 PDF 的代码——我已经有了。当我尝试运行应用程序时,我正在寻找此错误的答案 - 错误:任务 ':app:mergeDebugAssets' 的执行失败。
    • 查看您的调试文件夹并检查资产。它可能与他们的冲突。我确定您已经尝试过刷新项目等,但如果不这样做。如果这不起作用,请删除您的调试文件夹并重建
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2018-06-06
    • 2016-02-16
    • 2017-12-18
    相关资源
    最近更新 更多