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