错误:无法访问此文件检查位置或网络,然后在Android 6

时间:2017-03-03 10:52:06

标签: android android-studio android-6.0-marshmallow

我必须打开存储在资产文件夹中的pdf文件。当我尝试在Android 6中打开该文件时,它显示错误。但是,其他Android版本没问题。我认为这是许可的问题。请帮助纠正此错误。

这是我的代码 ....

dialBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {



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

            /** PDF reader code */
            File file = new File(Environment.getExternalStorageDirectory() + "/" + email+".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(getApplicationContext(), "Please install any pdf reader App.",
                        Toast.LENGTH_LONG).show();
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader")));
            }
        }
    });


}

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(email+".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);
    }
}

1 个答案:

答案 0 :(得分:1)

您是否包括:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 并且:清单中的<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我也找到了这段代码:

公共类SampleActivity扩展了Activity {

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

    }

    private void CopyReadAssets()
    {
        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);
        }
    }

}

以下是另一个答案的链接:Read a pdf file from assets folder