0

我正在努力制作一个简单的应用程序来下载互联网文件这是我的代码我已经从另一个问题中编辑了一些但是每当我运行我的应用程序时我仍然没有响应错误任何人都可以向我显示更正甚至不同的方式?

    package com.example.downloading;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.IntentService;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity {

    Button startBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startBtn = (Button)findViewById(R.id.download_button);
        startBtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {

                String url = "my stuff";
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setDescription("descrition");
                request.setTitle("title");

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "etc.mp3");


                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);

            }
        });
    }
}
4

1 回答 1

0
public void DownLoadTheMusic(){
        try {
            URL url = new URL("http://www.tagsinfosoft.com/android/shelf/Shelf_Cam.mp3");
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = Environment.getExternalStorageDirectory() + "/download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "Shelf_Cam.mp3");
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();


        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
        }
    }  


call this in a Asynctask




    public class downloadMusic extends AsyncTask<Integer, Integer, Integer>
        {


            @Override
            protected Integer doInBackground(Integer... params) {
                // TODO Auto-generated method stub

                try {

                    DownLoadTheMusic();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;

            }

            @Override
            protected void onPostExecute(Integer result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                Context context=shelf.this;
                pd.dismiss();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "Shelf_Cam.mp3")), "application/vnd.android.package-archive");
                startActivity(intent);  

            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                pd=ProgressDialog.show(shelf.this,"Updating","Please wait....." );
            }

        }

如果您从 Internet 下载小型音乐文件,请在按钮上执行此任务单击使用此任务。

于 2013-03-02T13:16:05.323 回答