java - Problems opening PDF from assets -
i have problem when trying open pdf (a.pdf) assets folder. thi java code:
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); } } }
this logcat:
11/10 11:52:42: launching app cold swapped changes. $ adb shell 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 process come online connected process 3259 on device nexus_5x_api_24 [emulator-5554] i/instantrun: instant run runtime started. android package com.tischer.alessandro.ecocardiochecklist, real application class 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) have incorrectly overridden 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) 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 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() @ android.app.activitythread.performlaunchactivity(activitythread.java:2646) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2707) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1460) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6077) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:865) @ 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() @ android.os.strictmode.onfileuriexposed(strictmode.java:1799) @ android.net.uri.checkfileuriexposed(uri.java:2346) @ android.content.intent.preparetoleaveprocess(intent.java:8933) @ android.content.intent.preparetoleaveprocess(intent.java:8894) @ android.app.instrumentation.execstartactivity(instrumentation.java:1517) @ android.app.activity.startactivityforresult(activity.java:4224) @ android.support.v4.app.basefragmentactivityjb.startactivityforresult(basefragmentactivityjb.java:48) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:75) @ android.app.activity.startactivityforresult(activity.java:4183) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:856) @ android.app.activity.startactivity(activity.java:4507) @ android.app.activity.startactivity(activity.java:4475) @ com.tischer.alessandro.ecocardiochecklist.bibliografia.oncreate(bibliografia.java:48) @ android.app.activity.performcreate(activity.java:6664) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1118) @ android.app.activitythread.performlaunchactivity(activitythread.java:2599) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2707) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1460) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6077) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:865) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:755)
i'm running android 7.0, app crashes older versions... where's problem?
you copy file application's private directory, no other apps can access. , try open file in app (it unable access file application's private directory).
to solve this, have few options:
- copy file public directory,
environment.getexternalstoragedirectory()
- implement
contentprovider
in app
1st way easier, , go way if doing it.
2nd way more correct, overheat here me.
Comments
Post a Comment