2017-02-15 7 views
0

最近AsynctaskでImageDownloaderメソッドを作成しましたが、現在のプロジェクトでオーディオをダウンロードするのとまったく同じメソッドが必要です。このメソッドの編集に親切にお手伝いできますか?AsyncTaskでオーディオダウンローダメソッドを作成する

(私はここにいくつかミスをしている場合、私はアンドロイドで新しいですとして私を修正してください)

これは私のImageDownloader AsynkTaskです:

public class ImageDownloader extends AsyncTask<String, String, String> { 

    private PowerManager.WakeLock mWakeLock; 

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

     PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); 
     mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
      getClass().getName()); 
     mWakeLock.acquire(); 
     mProgressDialog = new ProgressDialog(getActivity()); 
     mProgressDialog.setTitle("Downloading..."); 
     mProgressDialog.setMessage("Please wait while your file is downloading..."); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setCancelable(true); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgress(100); 



     mProgressDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... image_urls) { 
     int count = 0; 
     try { 

      URL url = new URL(image_urls[0]); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.connect();  
      InputStream is = conn.getInputStream(); 
      bmImg = BitmapFactory.decodeStream(is); 

      while(count != 100){ 
       publishProgress(""+count); 
       count +=5; 
      } 
     } 
     catch (IOException e) 
     {  
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(String... progress) { 
     Log.v("count",progress[0]); 
     mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    }  

@Override  
protected void onPostExecute(String args) { 

      mWakeLock.release();  
      File showPathInToast; 
      File filename; 
      try { 
       String path = Environment.getExternalStorageDirectory().toString(); 
       Log.i("in save()", "after mkdir"); 
       new File(path + "/Dastak/News").mkdirs(); 

       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
       String imageFileName = timeStamp; 

       filename = new File(path + "/Dastak/News/"+ imageFileName +".jpg"); 
       showPathInToast = new File(path + "/Dastak/News/"); 

       Log.i("in save()", "after file"); 
       FileOutputStream out = new FileOutputStream(filename); 
       Log.i("in save()", "after outputstream"); 
       bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 
       Log.i("in save()", "after outputstream closed"); 
       // MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), 
       //filename.getAbsolutePath(), filename.getName(),filename.getName()); 

       Toast.makeText(getActivity().getApplicationContext(), 
         "File has been saved to " + showPathInToast , Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      mProgressDialog.dismiss(); 
     } 
     } 

答えて

0

これは単純なAsyncTaskです。コンストラクタでは宛先ファイルを指定し、executeを呼び出すときにはurlを指定します。それは、何も返さなくなるまで単にInputStreamを読み込んで、FileOutputStreamを使って直接Destination Fileに書き込みます。

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

    File destination; 

    public FileDownloadTask(File destination) { 
     this.destination = destination; 
    } 

    @Override 
    protected Void doInBackground(String... urls) { 
     HttpURLConnection conn = null; 
     try { 
      URL url = new URL(urls[0]); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 

      if(!destination.exists()) { 
       if(!destination.mkdirs()) { 
        return null; 
       } 
      } 

      InputStream is = conn.getInputStream(); 
      FileOutputStream fos = new FileOutputStream(destination); 

      byte[] bytes = new byte[1024]; 

      while ((is.read(bytes) >= 0)) { 
       fos.write(bytes); 
      } 

      fos.flush(); 
      fos.close(); 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if(conn != null) { 
       conn.disconnect(); 
      } 
     } 
     return null; 
    } 
} 

明らかに、ProgressDialogロジックをもう一度追加できます。

関連する問題