从android中的资产文件夹打开pdf文件

时间:2013-11-22 02:09:58

标签: android pdf resources assets

我在资源文件夹中放置了一个pdf文件(bang.pdf),我需要在单击按钮时打开并显示来自assets文件夹的pdf。 代码是:

package com.example.pdfviewer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
    Button but1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but1 = (Button)findViewById(R.id.but1);

        but1.setOnClickListener(new OnClickListener()

        {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"hai" , Toast.LENGTH_SHORT).show();
                CopyReadAssets();

            }
        });
    }
    private void CopyReadAssets()
    {
        AssetManager assetManager = getAssets();
        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "bang.pdf");
        try
        {
            in = assetManager.open("bang.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() + "/bang.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);
        }
    }

    }

但是当我点击按钮时,会显示以下错误 错误: - android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.VIEW dat = file:///data/data/com.example.pdfviewer/files/bang.pdf typ = application / pdf}     在mainifestfile中: -     我提到                            我在我的设备中安装了adobe reader应用程序,仍然收到相同的错误消息。

请帮我这个

提前致谢

2 个答案:

答案 0 :(得分:1)

file:// android_asset仅适用于您的应用

您需要:

  1. 将文件复制到外部存储,或
  2. 将文件复制到内部存储空间并使用ContentProvider使其可供PDF查看应用
  3. 使用

    希望这有帮助。

答案 1 :(得分:0)

我看到答案在这里给出:

Read a pdf file from assets folder

(这几乎是添加了附加项目的问题,缺少权限WRITE_EXTERNAL_STORAGE的请求)。 file://似乎没问题,指的是外部存储。

也许有更好的方法而不是复制到外面(按照Siddharth Vyas的建议复制到内部存储+ ContentProvider?)。

相关问题