简体   繁体   English

ASyncTask-我想通过按钮从互联网下载文件

[英]ASyncTask - I want to download a file from the internet through a button

I'm still new to the whole coding thing, but I would like some help with ASyncTask and how to allow it to download my app from the internet straight to my phone through notification or just in the SD Card. 我仍然对整个编码工作还不熟悉,但是我想获得有关ASyncTask的帮助,以及如何通过通知或通过SD卡将其从互联网直接下载到我的手机中。

I already have a button inside the activity_main.xml layout which is: 我已经在activity_main.xml布局中有一个按钮,该按钮是:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/UpdateButton"
    android:text="@string/Update"
    android:background="#6c0005"
    android:textColor="#ffffff"
    android:textSize="12dp" />

In my MainActivity.Java class I just have right now a button connecting to a string and intent to redirect it to a webview to download it: 现在在我的MainActivity.Java类中,我有一个连接到字符串的按钮,并打算将其重定向到Web视图以下载它:

case R.id.UpdateButton:
    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
    Uri.parse(getString(R.string.UpdateAV)));
    startActivity(browserIntent);
    break;

but instead of that, I would like the file to download straight to the phone or a progress bar saying it is downloading to the External memory or SD Card. 但是我要将该文件直接下载到手机或进度条,以表明它正在下载到外部存储器或SD卡。

Any help would be appreciated. 任何帮助,将不胜感激。

this is just an example that how you can download a file, you would have to write an equivalent code for this in Android if you want to use AsyncTask , in which you will do the same work in the doInBacground() method. 这只是一个示例,说明如何下载文件,如果要使用AsyncTask ,则必须在Android中为此编写等效的代码,在该代码中,您将在doInBacground()方法中执行相同的工作。

public class DownloadTest {

public static void main(String[] args) {

    Thread thread = new Thread(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            try {
                dowanloadFile(new URL("some url"), new File("some file"));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    thread.start();


   }

private static void dowanloadFile(URL url, File file){
    try {
        FileUtils.copyURLToFile(url, file );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

this code code uses commons-io-2.4.jar library, 该代码使用commons-io-2.4.jar库,

you can get little more code her Example 你可以得到更多的代码她的例子

you can get path to download at SD card like this 您可以像这样在SD卡上下载路径

final String path = Environment.getExternalStorageDirectory().getAbsolutePath();

with AsycnTask it would be like this 使用AsycnTask会像这样

private class MtDownloadTask extends AsyncTask<Void, Void ,Void>{

    URL someUrl = new URL("your url String"); //
    File someFile = new File("your file, where you want to save the data");

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
        FileUtils.copyURLToFile(url, file);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

}

make sure you do have internet permission to your app, to do that add this line to your AndroidManifest.xml 确保您确实拥有应用程序的互联网许可,然后将此行添加到AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM