我创建了一个android应用程序。在其中我必须下载和查看pdf文件。当我在模拟器中运行我的应用程序时,如果我选择下载它是空闲的,并且如果我选择视图,它会重定向到Adobe Reader,但显示“文档”路径无效”。我想念什么?

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 the menu; this adds items to the action bar if it is 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 to 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>


我的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):     at java.io.File.createNewFile(File.java:946)
03-21 03:55:53.532: W/System.err(1571):     at com.example.library.Screen8$DownloadFile.doInBackground(Screen8.java:66)
03-21 03:55:53.552: W/System.err(1571):     at com.example.library.Screen8$DownloadFile.doInBackground(Screen8.java:1)
03-21 03:55:53.572: W/System.err(1571):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-21 03:55:53.592: W/System.err(1571):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-21 03:55:53.592: W/System.err(1571):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-21 03:55:53.632: W/System.err(1571):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-21 03:55:53.642: W/System.err(1571):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-21 03:55:53.652: W/System.err(1571):     at 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):     at libcore.io.Posix.open(Native Method)
03-21 03:55:53.712: W/System.err(1571):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
03-21 03:55:53.732: W/System.err(1571):     at 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):     at libcore.io.IoBridge.open(IoBridge.java:409)
03-21 03:56:06.292: W/System.err(1571):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
03-21 03:56:06.302: W/System.err(1571):     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
03-21 03:56:06.322: W/System.err(1571):     at com.example.library.FileDownloder.downloadFile(FileDownloder.java:26)
03-21 03:56:06.322: W/System.err(1571):     at com.example.library.Screen8$DownloadFile.doInBackground(Screen8.java:70)
03-21 03:56:06.332: W/System.err(1571):     at com.example.library.Screen8$DownloadFile.doInBackground(Screen8.java:1)
03-21 03:56:06.332: W/System.err(1571):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-21 03:56:06.332: W/System.err(1571):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-21 03:56:06.332: W/System.err(1571):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-21 03:56:06.332: W/System.err(1571):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-21 03:56:06.372: W/System.err(1571):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-21 03:56:06.372: W/System.err(1571):     at 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):     at libcore.io.Posix.open(Native Method)
03-21 03:56:06.412: W/System.err(1571):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
03-21 03:56:06.422: W/System.err(1571):     at 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

最佳答案

对于View,这应该可以为您提供帮助。

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


}

09-11 22:02