本文介绍了Android的 - 从资产Access文件\ PDF显示效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索引用存储在资产文件目录下一个名为 myfile.pdf 。我试图做到这一点,如下所示:

 档案文件=新的文件(android_assest / myfile.pdf);
Log.d(MyTag的,+ file.isFile());
 

不知怎的,我得到 myfile.pdf 做存在于资产目录。我验证它使用 getAssets()。表() Log.d()在返回的每一个元素数组。

更多,其中,我想获得一个引用的PDF文件,然后使用任何PDF阅读器,它是在设备上已安装,以查看PDF。

我想,既然previous问题(检索参考文件)返回则下一个文档片断code失败:

 意向书我=新的意图(Intent.ACTION_VIEW,
    Uri.parse(文件:///android_asset/myfile.pdf));
startActivity(ⅰ);
 

任何人有一个线索,为什么我无法检索引用文件?为什么我不能使用已安装的PDF查看器来显示PDF(检索参考PDF文件之后)?

感谢。

解决方案

由于巴拉克说,你可以把它复制出资产的内部存储或SD卡,并从那里通过内置的PDF的应用程序打开它。

下面的代码片段会帮助你。(我已经更新了这个code编写,并从内部存储读取文件。

但我不推荐这种方式,因为PDF文件可以超过100MB的大小也比较多。

因此​​,它不建议以保存大量文件导入到内部存储

另外,还要确保在保存文件到您使用内部存储

  openFileOutput(file.getName(),Context.MODE_WORLD_READABLE);
 

那么只有其他应用程序可以读取它。

检查下面的代码片断。

 包org.sample;

进口的java.io.File;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;

进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.content.res.AssetManager;
进口android.net.Uri;
进口android.os.Bundle;
进口android.util.Log;

公共类SampleActivity扩展活动
{

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        CopyReadAssets();

    }

    私人无效CopyReadAssets()
    {
        AssetManager assetManager = getAssets();

        在的InputStream = NULL;
        出的OutputStream = NULL;
        档案文件=新的文件(getFilesDir(),git.pdf);
        尝试
        {
            在= assetManager.open(git.pdf);
            OUT = openFileOutput(file.getName(),Context.MODE_WORLD_READABLE);

            的CopyFile(IN,OUT);
            附寄();
            在= NULL;
            了out.flush();
            out.close();
            OUT = NULL;
        }赶上(例外五)
        {
            Log.e(标签,e.getMessage());
        }

        意向意图=新的意图(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse(文件://+ getFilesDir()+/git.pdf),
                应用程序/ PDF格式);

        startActivity(意向);
    }

    私人无效的CopyFile(在的InputStream,OutputStream的了)抛出IOException异常
    {
        byte []的缓冲区=新的字节[1024];
        INT读取;
        而((读= in.read(缓冲))!= -1)
        {
            out.write(缓冲,0,读);
        }
    }

}
 

请确保包括

<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>

在清单

I am trying to retrieve a reference to a file stored in the assets directory to a file named myfile.pdf. I have tried to do it as follows:

File file = new File("android_assest/myfile.pdf);
Log.d("myTag", "" + file.isFile());

Somehow, I get false when the myfile.pdf do exists in the assets directory. I verified it using getAssets().list("") and Log.d() each element in the returned array.

More of which, I am trying to get a reference to a PDF file and then use any PDF viewer, which is already installed on the device, in order to view the PDF.

I guess that since the previous issue (retrieving a reference to the file) returns false then the next snipped code fails:

Intent i = new Intent(Intent.ACTION_VIEW,
    Uri.parse("file:///android_asset/myfile.pdf"));
startActivity(i);

Anyone has a clue why I am unable to retrieve a reference to the file? and why I cannot use already installed PDF viewer to display a PDF (after retrieving a reference to the PDF file)?

Thanks.

解决方案

As Barak said you can copy it out of assets to internal storage or the SD card and open it from there using inbuilt pdf applications.

Following Snippet will help you.(I have updated this code to write to and read files from internal storage.

But i dont recommend this approach because pdf file can be more than 100mb in size.

So its not recommended to save that huge file into internal storage

Also make sure while saving file to internal storage you use

openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

Then only other applications can read it.

Check following snippet.

package org.sample;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class SampleActivity extends 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(), "git.pdf");
        try
        {
            in = assetManager.open("git.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() + "/git.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);
        }
    }

}

Make sure to include

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in manifest

这篇关于Android的 - 从资产Access文件\ PDF显示效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:46