使用intent打开assets文件夹中的文件

时间:2018-05-28 11:08:28

标签: java android android-intent

您好我想使用intent打开文件中存在的.pdf文件,但它在下一行中给出了2个错误

File file = new File(getContext().getAssets().open("assets/test.pdf"));

错误
1.未处理的java.IO.Exception。
2。getAssets()可能会产生java.lang.NullPointerException
这里是片段中的代码

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (position == 0) {
        File file = new File(getContext().getAssets().open("assets/test.pdf"));
        if (file .exists())
        {
            Uri path = Uri.fromFile(file );
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path , "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            try
            {
                startActivity(pdfIntent ); }
            catch (ActivityNotFoundException e)
            {
                Toast.makeText(getActivity(), "Please install a pdf file viewer",
                        Toast.LENGTH_LONG).show();
            }
        }
}
}

3 个答案:

答案 0 :(得分:4)

    File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + "abc.pdf");
    if (!fileBrochure.exists())
    {
         CopyAssetsbrochure();
    } 

    /** PDF reader code */
    File file = new File(Environment.getExternalStorageDirectory() + "/" + "abc.pdf");      

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try 
    {
        getApplicationContext().startActivity(intent);
    } 
    catch (ActivityNotFoundException e) 
    {
         Toast.makeText(SecondActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }
}

//method to write the PDFs file to sd card
    private void CopyAssetsbrochure() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try 
        {
            files = assetManager.list("");
        } 
        catch (IOException e)
        {
            Log.e("tag", e.getMessage());
        }
        for(int i=0; i<files.length; i++)
        {
            String fStr = files[i];
            if(fStr.equalsIgnoreCase("abc.pdf"))
            {
                InputStream in = null;
                OutputStream out = null;
                try 
                {
                  in = assetManager.open(files[i]);
                  out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                  copyFile(in, out);
                  in.close();
                  in = null;
                  out.flush();
                  out.close();
                  out = null;
                  break;
                } 
                catch(Exception e)
                {
                    Log.e("tag", e.getMessage());
                } 
            }
        }
    }

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

您无法直接从assets文件夹打开pdf文件。首先必须将文件从assets文件夹写入SD卡,然后从SD卡读取

答案 1 :(得分:3)

尝试使用文件提供程序

 Intent intent = new Intent(Intent.ACTION_VIEW);

    // set flag to give temporary permission to external app to use your FileProvider
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    // generate URI, I defined authority as the application ID in the Manifest, the last param is file I want to open
    String uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file);

    // I am opening a PDF file so I give it a valid MIME type
    intent.setDataAndType(uri, "application/pdf");

    // validate that the device can open your File!
    PackageManager pm = getActivity().getPackageManager();
    if (intent.resolveActivity(pm) != null) {
        startActivity(intent);
    }

答案 2 :(得分:1)

要将文件从资源投放到其他应用,您需要使用提供商。

Google for CommonsWare的StreamProvider。