有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android访问资产中的文件\PDF显示

我正在尝试检索assets目录中存储的文件对名为myfile.pdf的文件的引用。我已尝试按以下方式进行:

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

不知何故,当myfile.pdfdo存在于assets目录中时,我得到了false。我使用返回数组中的getAssets().list("")Log.d()每个元素来验证它

更重要的是,我正在尝试获取对PDF文件的引用,然后使用设备上已安装的任何PDF查看器来查看PDF

我猜想,由于上一个问题(检索对文件的引用)返回false,因此下一个被剪断的代码将失败:

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

有人知道我为什么无法检索该文件的引用吗?为什么我不能使用已经安装的PDF viewer来显示PDF(在检索到PDF文件的引用后)

谢谢


共 (3) 个答案

  1. # 1 楼答案

    你不能那样做。apk中没有目录结构,它只是数据。框架知道如何访问它(getAssets),但不能将其作为目录中的文件进行查找

    您可以将其作为输入流打开

    in = new BufferedReader(new InputStreamReader(activity.getAssets().open(myfile.pdf))); 
    

    或者,您可以将其从资产中复制到内部存储器或SD卡,并在那里访问

  2. # 2 楼答案

    正如巴拉克所说,您可以将其从资产复制到内部存储或SD卡,然后使用内置pdf应用程序从那里打开

    下面的代码片段将对您有所帮助。 (我已更新此代码,以便从内部存储器写入和读取文件

    但我不推荐这种方法,因为pdf文件的大小可能超过100mb

    因此,不建议将这个巨大的文件保存到内部存储器中

    在将文件保存到您使用的内部存储器时,还要确保

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

    然后只有其他应用程序才能读取它

    检查以下代码段

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

    确保包括

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

    明显地

  3. # 3 楼答案

    比如说Vipul Shah,但是对于外部

    import android.app.Activity;
    import android.content.Intent;
    import android.content.res.AssetManager;
    import android.net.Uri;
    import android.os.Environment;
    import android.os.Bundle;
    import android.util.Log;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            copyReadAssets();
        }
    
    
        private void copyReadAssets()
        {
            AssetManager assetManager = getAssets();
    
            InputStream in = null;
            OutputStream out = null;
    
            String strDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ File.separator + "Pdfs";
            File fileDir = new File(strDir);
            fileDir.mkdirs();   // crear la ruta si no existe
            File file = new File(fileDir, "example2.pdf");
    
    
    
            try
            {
    
                in = assetManager.open("example.pdf");  //leer el archivo de assets
                out = new BufferedOutputStream(new FileOutputStream(file)); //crear el archivo
    
    
                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://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + "Pdfs" + "/example2.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);
            }
        }
    }
    

    更改代码的以下部分:

    out = new BufferedOutputStream(new FileOutputStream(file));
    

    前面的例子是PDF的例子,例如to。文本

    FileOutputStream fos = new FileOutputStream(file);