首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:无法访问此文件,请检查位置或网络,然后在Android 6中重试

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

Stack Overflow用户
提问于 2017-03-03 18:52:06
回答 1查看 2.1K关注 0票数 0

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

这是我的代码...

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-03 19:31:31

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

我还找到了下面的代码:

公共类SampleActivity扩展了Activity {

代码语言:javascript
运行
复制
    @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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42576934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档