2016-11-10 11 views
-2

Ich habe ein Problem, wenn ich versuche, ein PDF (a.pdf) aus meinem Assets-Ordner zu öffnen. Thi ist der Java-Code:Probleme beim Öffnen von PDF aus Assets

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

} 

Dies ist die logcat ist:

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)  

I Android 7.0 laufen bin, aber die App stürzt auch mit älteren Versionen ... Wo ist das Problem?

+0

* Wo ist das Problem? * FileUriExposedException - sieht aus wie selbsterklärende Ausnahme ... bereits oft gefragt ... offensichtliche Lösung ist die Verwendung des FileProvider – Selvin

+0

Sorry, aber ich bin neu in Android-Entwicklung ... Ich verstehe immer noch nicht, was ich im Code ändern muss ... könntest du mir helfen? –

+0

Suche im Internet auf FileUriExposedException – Selvin

Antwort

0

Sie kopieren die Datei in das private Verzeichnis Ihrer Anwendung, auf das keine anderen Apps zugreifen können. Und dann versuchen Sie, diese Datei in einer anderen App zu öffnen (es ist einfach nicht möglich, aus dem privaten Verzeichnis Ihrer Anwendung auf diese Datei zuzugreifen).

Um dies zu lösen, haben Sie einige Optionen:

  1. Kopieren Sie die Datei in einem öffentlichen Verzeichnis, wie Environment.getExternalStorageDirectory()
  2. Implementieren einer ContentProvider in Ihrer App

erste Weg ist einfacher, und Ich würde diesen Weg gehen, wenn ich es tun würde.
2. Weg ist mehr richtig, aber offensichtlich ist hier eine Überhitzung für mich.

+0

Ich möchte den Inhaltsanbieter implementieren, auch weil ich diese App erstelle, weil ich lernen möchte, also möchte ich die Dinge so gut wie möglich machen. Könnten Sie mir helfen, den Inhaltsanbieter einzufügen? –

+0

Ich habe folgendes gefunden: [link] (http://Stackoverflow.com/a/21063373/7123499), das scheint toll zu sein, aber es funktioniert nicht für mich ... –

+0

@AlessandroTischer, könnte ich, aber es passt nicht vollständig zu Ihrem Fragenthema. Sie sollten eine neue Frage dafür stellen, denke ich. Aber zuerst solltest du darüber googeln, da es bereits einen Ton von Lektionen über Inhaltsanbieter im Internet gibt, und es gibt keinen Grund für mich, den Code für dich aus einer der Lektionen –

Verwandte Themen