java - Document path is not valid error in android emulator -
im creating android application.in have download , view pdf files.and when run app in emulator, if select download idle , if select view gets redirected adobe reader displays "document path not valid".what missing?
screen8.java
package com.example.library; import java.io.file; import java.io.ioexception; import android.app.activity; import android.content.activitynotfoundexception; import android.content.intent; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.os.environment; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast; public class screen8 extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_screen8); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.screen8, menu); return true; } public void download(view v) { new downloadfile().execute("http://maven.apache.org/maven-1.x/maven.pdf", "maven.pdf"); } public void view(view v) { file pdffile = new file(environment.getexternalstoragedirectory() + "/testthreepdf/" + "maven.pdf"); // -> filename = maven.pdf uri path = uri.fromfile(pdffile); intent pdfintent = new intent(intent.action_view); pdfintent.setdataandtype(path, "application/pdf"); pdfintent.setflags(intent.flag_activity_clear_top); try{ startactivity(pdfintent); }catch(activitynotfoundexception e){ toast.maketext(screen8.this, "no application available view pdf", toast.length_short).show(); } } private class downloadfile extends asynctask<string, void, void>{ @override protected void doinbackground(string... strings) { string fileurl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf string filename = strings[1]; // -> maven.pdf string extstoragedirectory = environment.getexternalstoragedirectory().tostring(); file folder = new file(extstoragedirectory, "testthreepdf"); folder.mkdir(); file pdffile = new file(folder, filename); try{ pdffile.createnewfile(); }catch (ioexception e){ e.printstacktrace(); } filedownloder.downloadfile(fileurl, pdffile); return null; } } }
filedownloder.java
package com.example.library; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; public class filedownloder { private static final int megabyte = 1024 * 1024; public static void downloadfile(string fileurl, file directory){ try { url url = new url(fileurl); httpurlconnection urlconnection = (httpurlconnection)url.openconnection(); //urlconnection.setrequestmethod("get"); //urlconnection.setdooutput(true); urlconnection.connect(); inputstream inputstream = urlconnection.getinputstream(); fileoutputstream fileoutputstream = new fileoutputstream(directory); int totalsize = urlconnection.getcontentlength(); byte[] buffer = new byte[megabyte]; int bufferlength = 0; while((bufferlength = inputstream.read(buffer))>0 ){ fileoutputstream.write(buffer, 0, bufferlength); } fileoutputstream.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
android manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.library" android:versioncode="1" android:versionname="1.0" > <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" > </uses-permission> <uses-permission android:name="android.permission.read_phone_state" > </uses-permission> <uses-sdk android:minsdkversion="17" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".screen1" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".screen2" android:label="@string/title_activity_screen2" android:parentactivityname=".screen1" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> <meta-data android:name="android.support.parent_activity" android:value="com.example. library.screen1" /> </activity> <activity android:name=".screen3" android:label="@string/title_activity_screen3" android:parentactivityname=".screen2" > <meta-data android:name="android.support.parent_activity" android:value="com.example.library.screen2" /> </activity> <activity android:name=".second" android:label="@string/title_activity_second" > </activity> <activity android:name=".screen4" android:label="@string/title_activity_screen4" android:parentactivityname=".screen3" > <meta-data android:name="android.support.parent_activity" android:value="com.example.library.screen3" /> </activity> <activity android:name=".screen5" android:label="@string/title_activity_screen5" android:parentactivityname=".screen4" android:screenorientation="portrait" > <meta-data android:name="android.support.parent_activity" android:value="com.example.library.screen4" /> </activity> <activity android:name=".screen6" android:label="@string/title_activity_screen6" > </activity> <activity android:name=".screen7" android:label="@string/title_activity_screen7" > </activity> <activity android:name=".screen5ii" android:label="@string/title_activity_screen5ii" > </activity> <activity android:name=".screen5iii" android:label="@string/title_activity_screen5iii" > </activity> <activity android:name=".screen5iv" android:label="@string/title_activity_screen5iv" > </activity> <activity android:name=".screen8" android:label="@string/title_activity_screen8" > </activity> <activity android:name=".filedownloader" android:label="@string/title_activity_file_downloader" > </activity> </application> </manifest>
activity_screen8.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.library.screen8" > <button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="15dp" android:text="download" android:onclick="download" /> <button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/button1" android:layout_margintop="38dp" android:text="view" android:onclick="view" /> </relativelayout>
my logcat
03-21 03:55:53.492: w/system.err(1571): java.io.ioexception: open failed: eacces (permission denied) 03-21 03:55:53.522: w/system.err(1571): @ java.io.file.createnewfile(file.java:946) 03-21 03:55:53.532: w/system.err(1571): @ com.example.library.screen8$downloadfile.doinbackground(screen8.java:66) 03-21 03:55:53.552: w/system.err(1571): @ com.example.library.screen8$downloadfile.doinbackground(screen8.java:1) 03-21 03:55:53.572: w/system.err(1571): @ android.os.asynctask$2.call(asynctask.java:288) 03-21 03:55:53.592: w/system.err(1571): @ java.util.concurrent.futuretask.run(futuretask.java:237) 03-21 03:55:53.592: w/system.err(1571): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 03-21 03:55:53.632: w/system.err(1571): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 03-21 03:55:53.642: w/system.err(1571): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 03-21 03:55:53.652: w/system.err(1571): @ java.lang.thread.run(thread.java:841) 03-21 03:55:53.672: w/system.err(1571): caused by: libcore.io.errnoexception: open failed: eacces (permission denied) 03-21 03:55:53.702: w/system.err(1571): @ libcore.io.posix.open(native method) 03-21 03:55:53.712: w/system.err(1571): @ libcore.io.blockguardos.open(blockguardos.java:110) 03-21 03:55:53.732: w/system.err(1571): @ java.io.file.createnewfile(file.java:939) 03-21 03:55:53.732: w/system.err(1571): ... 8 more 03-21 03:55:54.382: d/dalvikvm(1571): gc_for_alloc freed 268k, 8% free 4302k/4636k, paused 165ms, total 166ms 03-21 03:56:06.272: w/system.err(1571): java.io.filenotfoundexception: /storage/sdcard/testthreepdf/maven.pdf: open failed: eacces (permission denied) 03-21 03:56:06.292: w/system.err(1571): @ libcore.io.iobridge.open(iobridge.java:409) 03-21 03:56:06.292: w/system.err(1571): @ java.io.fileoutputstream.<init>(fileoutputstream.java:88) 03-21 03:56:06.302: w/system.err(1571): @ java.io.fileoutputstream.<init>(fileoutputstream.java:73) 03-21 03:56:06.322: w/system.err(1571): @ com.example.library.filedownloder.downloadfile(filedownloder.java:26) 03-21 03:56:06.322: w/system.err(1571): @ com.example.library.screen8$downloadfile.doinbackground(screen8.java:70) 03-21 03:56:06.332: w/system.err(1571): @ com.example.library.screen8$downloadfile.doinbackground(screen8.java:1) 03-21 03:56:06.332: w/system.err(1571): @ android.os.asynctask$2.call(asynctask.java:288) 03-21 03:56:06.332: w/system.err(1571): @ java.util.concurrent.futuretask.run(futuretask.java:237) 03-21 03:56:06.332: w/system.err(1571): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 03-21 03:56:06.332: w/system.err(1571): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 03-21 03:56:06.372: w/system.err(1571): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 03-21 03:56:06.372: w/system.err(1571): @ java.lang.thread.run(thread.java:841) 03-21 03:56:06.382: w/system.err(1571): caused by: libcore.io.errnoexception: open failed: eacces (permission denied) 03-21 03:56:06.412: w/system.err(1571): @ libcore.io.posix.open(native method) 03-21 03:56:06.412: w/system.err(1571): @ libcore.io.blockguardos.open(blockguardos.java:110) 03-21 03:56:06.422: w/system.err(1571): @ libcore.io.iobridge.open(iobridge.java:393) 03-21 03:56:06.422: w/system.err(1571): ... 11 more 03-21 03:56:25.982: w/egl_emulation(1571): eglsurfaceattrib not implemented
for view, should you.
private void loaddocinreader(string doc) throws activitynotfoundexception, exception { try { intent intent = new intent(); intent.setpackage("com.adobe.reader"); intent.setdataandtype(uri.parse(doc), "application/pdf"); startactivity(intent); } catch (activitynotfoundexception activitynotfoundexception) { activitynotfoundexception.printstacktrace(); throw activitynotfoundexception; } catch (exception otherexception) { otherexception.printstacktrace(); throw otherexception; }
}
Comments
Post a Comment