从资产打开PDF的问题

时间:2016-11-10 10:59:29

标签: java android pdf assets

尝试从我的资源文件夹中打开pdf(a.pdf)时出现问题。 这是java代码:

package com.tischer.alessandro.ecocardiochecklist;


import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Bibliografia extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bibliografia);
        AssetManager assetManager = getAssets();

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

}

这是logcat:

11/10 11:52:42: Launching app
Cold swapped changes.
$ adb shell am start -n "com.tischer.alessandro.ecocardiochecklist/com.tischer.alessandro.ecocardiochecklist.SplashScreen" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 3259 on device Nexus_5X_API_24 [emulator-5554]
I/InstantRun: Instant Run Runtime started. Android package is com.tischer.alessandro.ecocardiochecklist, real application class is null.

              [ 11-10 10:52:44.162  1489: 1510 D/         ]
              HostConnection::get() New Host Connection established 0x8d361440, tid 1510
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/gralloc_ranchu: Gralloc pipe failed

                  [ 11-10 10:52:45.193  3259: 3259 D/         ]
                  HostConnection::get() New Host Connection established 0x9d027140, tid 3259


                  [ 11-10 10:52:45.239  3259: 3278 D/         ]
                  HostConnection::get() New Host Connection established 0x9d0d6d40, tid 3278
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
D/OpenGLRenderer: endAllActiveAnimators on 0x90937d80 (RippleDrawable) with handle 0x9d07eb80
E/tag: MODE_WORLD_READABLE no longer supported
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.tischer.alessandro.ecocardiochecklist, PID: 3259
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tischer.alessandro.ecocardiochecklist/com.tischer.alessandro.ecocardiochecklist.Bibliografia}: android.os.FileUriExposedException: file:///data/user/0/com.tischer.alessandro.ecocardiochecklist/files/a.pdf exposed beyond app through Intent.getData()
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: android.os.FileUriExposedException: file:///data/user/0/com.tischer.alessandro.ecocardiochecklist/files/a.pdf exposed beyond app through Intent.getData()
                      at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
                      at android.net.Uri.checkFileUriExposed(Uri.java:2346)
                      at android.content.Intent.prepareToLeaveProcess(Intent.java:8933)
                      at android.content.Intent.prepareToLeaveProcess(Intent.java:8894)
                      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
                      at android.app.Activity.startActivityForResult(Activity.java:4224)
                      at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                      at android.app.Activity.startActivityForResult(Activity.java:4183)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                      at android.app.Activity.startActivity(Activity.java:4507)
                      at android.app.Activity.startActivity(Activity.java:4475)
                      at com.tischer.alessandro.ecocardiochecklist.Bibliografia.onCreate(Bibliografia.java:48)
                      at android.app.Activity.performCreate(Activity.java:6664)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我正在运行Android 7.0,但应用程序也崩溃了旧版本......问题出在哪里?

2 个答案:

答案 0 :(得分:0)

您将文件复制到应用程序的私人目录中,其他应用程序无法访问该目录。然后你尝试在另一个应用程序中打开这个文件(它无法从你的应用程序的私人目录中访问该文件)。

要解决此问题,您几乎没有选择:

  1. 将文件复制到某个公共目录,例如Environment.getExternalStorageDirectory()
  2. 在您的应用中实施ContentProvider
  3. 第一种方式更容易,如果我这样做,我会这样做。
    第二种方式是正确,但显然这对我来说是过热。

答案 1 :(得分:0)

我认为您应该首先获得上下文,

Context context=getApplicationContext();

 AssetManager assetManager = context.getAssets();