Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio 在android上下载文档_Android Studio_Android Asynctask_Download_Httpurlconnection_Fileinputstream - Fatal编程技术网

Android studio 在android上下载文档

Android studio 在android上下载文档,android-studio,android-asynctask,download,httpurlconnection,fileinputstream,Android Studio,Android Asynctask,Download,Httpurlconnection,Fileinputstream,我想从我在网上抓取的链接下载文档,我有一个listview侦听器,所以当我按下列表中的一个项目时,它将返回一个url,我将它设置为getLink将是我想下载并设置filename的pdf文件的链接 getLink = http://www.phivolcs.dost.gov.ph/images/Flyer-eq-and-eq-hazards.pdf filename = Flyer-eq-and-eq-hazards.pdf 所以我在listview监听器中调用它,它应该下载,但不起作用。 n

我想从我在网上抓取的链接下载文档,我有一个listview侦听器,所以当我按下列表中的一个项目时,它将返回一个url,我将它设置为
getLink
将是我想下载并设置
filename
的pdf文件的链接

getLink = http://www.phivolcs.dost.gov.ph/images/Flyer-eq-and-eq-hazards.pdf
filename = Flyer-eq-and-eq-hazards.pdf
所以我在listview监听器中调用它,它应该下载,但不起作用。
newdownloadfile().execute(getLink,filename)

我有一些错误,比如
java.io.IOException:open failed:enoint(没有这样的文件或目录)

内因

private class DownloadFile extends AsyncTask<String, Void, Void>{

   @Override
   protected Void doInBackground(String... strings) {
       String fileUrl = strings[0];   // -> getLink = http://www.phivolcs.dost.gov.ph/images/Flyer-eq-and-eq-hazards.pdf
       String fileName = strings[1];  // -> Flyer-eq-and-eq-hazards.pdf
       String extStorageDirectory = Environment.getDataDirectory().toString();
       File folder = new File(extStorageDirectory, "testthreepdf");
       folder.mkdir();

       File pdfFile = new File(folder, fileName);

       try{
           pdfFile.createNewFile();
       }catch (IOException e){
           e.printStackTrace();
       }
       FileDownloader.downloadFile(fileUrl, pdfFile);
       return null;
   }
}
显示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.boneyflesh.homepage">

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="AllowBackup">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" />
    <!--
 ATTENTION: This was auto-generated to add Google Play services to your project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
    -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity android:name=".GeohazardResults" />
    <activity android:name=".MineralResults" />
    <activity android:name=".LandformResults" />
    <activity android:name=".Downloads"></activity>
</application>
Asynktask

private class DownloadFile extends AsyncTask<String, Void, Void>{

   private Context myContextRef;

   public DownloadFile(Context context) {
       myContextRef = context;
   }


   @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 = myContextRef.getFilesDir().toString();
       //String extStorageDirectory = Environment.getDataDirectory().toString();
       File folder = new File(extStorageDirectory, "testthreepdf");
       folder.mkdir();

       File pdfFile = new File(folder, filename);

       try{
           pdfFile.createNewFile();
       }catch (IOException e){
           e.printStackTrace();
       }
       FileDownloader.downloadFile(getLink, pdfFile);
       return null;
   }
   }
私有类下载文件扩展异步任务{
私有上下文myContextRef;
公共下载文件(上下文){
myContextRef=上下文;
}
@凌驾
受保护的Void doInBackground(字符串…字符串){
//String fileUrl=strings[0];//->http://maven.apache.org/maven-1.x/maven.pdf
//字符串文件名=字符串[1];//->maven.pdf
字符串extStorageDirectory=myContextRef.getFilesDir().toString();
//字符串extStorageDirectory=Environment.getDataDirectory().toString();
文件夹=新文件(extStorageDirectory,“testthreepdf”);
folder.mkdir();
File pdfFile=新文件(文件夹、文件名);
试一试{
pdfFile.createNewFile();
}捕获(IOE异常){
e、 printStackTrace();
}
下载文件(getLink,pdfFile);
返回null;
}
}
下载文件
保持不变

换行

String extStorageDirectory = Environment.getDataDirectory().toString();

其中,
context
是您需要提供给
AsyncTask
上下文


Environment.getDataDirectory()
是系统范围内的用户数据目录,当
ontext.getFilesDir()时,您不能在其中创建文件
是应用程序的数据目录,您可以在其中创建文件。

对不起,我不太熟悉
AsyncTask
尝试将其更改为
String extStorageDirectory=context.getfiledir().toString()表示无法解析符号“上下文”,我的选项是创建局部变量、字段、参数和引用。我该怎么办?您需要将
上下文
放入
异步任务
——有几种方法可以做到这一点,但您可以找到一种方法在堆栈溢出时使用
异步任务
。您的代码使用
异步任务
。它是
下载文件
类的基类。你需要为
下载文件
的构造函数添加
上下文
。我会编辑我的代码供你检查,,我不知道我是否做对了。不,我没有下载文件,而是创建了一个
浏览器意图
,因此每次单击链接时,它都会打开一个浏览器,当有人发布我能得到的答案时,我仍然会实现这一点。
private class DownloadFile extends AsyncTask<String, Void, Void>{

   private Context myContextRef;

   public DownloadFile(Context context) {
       myContextRef = context;
   }


   @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 = myContextRef.getFilesDir().toString();
       //String extStorageDirectory = Environment.getDataDirectory().toString();
       File folder = new File(extStorageDirectory, "testthreepdf");
       folder.mkdir();

       File pdfFile = new File(folder, filename);

       try{
           pdfFile.createNewFile();
       }catch (IOException e){
           e.printStackTrace();
       }
       FileDownloader.downloadFile(getLink, pdfFile);
       return null;
   }
   }
String extStorageDirectory = Environment.getDataDirectory().toString();
String extStorageDirectory = context.getFilesDir().toString();