我有这个类从服务器下载图像,并且我在我的主要活动中运行它
public class imgDownloader extends AsyncTask<Void, Integer, Void> {
private ProgressBar pb;
private String url;
private Button save;
private Context c;
private int progress;
private ImageView img;
private Bitmap bmp;
private TextView percent;
private ImageLoaderListener listener;
/*--- constructor ---*/
public ImageDownloader(String url, ProgressBar pb, Button save,
ImageView img, TextView percent, Context c, Bitmap bmp, ImageLoaderListener listener) {
/*--- we need to pass some objects we are going to work with ---*/
this.url = url;
this.pb = pb;
this.save = save;
this.c = c;
this.img = img;
this.percent = percent;
this.bmp = bmp;
this.listener = listener;
}
/*--- we need this interface for keeping the reference to our Bitmap from the MainActivity.
* Otherwise, bmp would be null in our MainActivity*/
public interface ImageLoaderListener {
void onImageDownloaded(Bitmap bmp);
}
@Override
protected void onPreExecute() {
//progress = 0;
//pb.setVisibility(View.VISIBLE);
//percent.setVisibility(View.VISIBLE);
//Toast.makeText(c, "starting download", Toast.LENGTH_SHORT).show();
Log.d("Script", "//" + "starting download");
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
bmp = getBitmapFromURL(url);
/*
while (progress < 10) {
progress += 1;
publishProgress(progress);
SystemClock.sleep(200);
}
*/
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
/*--- show download progress on main UI thread---*/
// pb.setProgress(values[0]);
// percent.setText(values[0] + "%");
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
if (listener != null) {
listener.onImageDownloaded(bmp);
}
img.setImageBitmap(bmp);
//save.setEnabled(true);
//Toast.makeText(c, "download complete", Toast.LENGTH_SHORT).show();
Log.d("Script", "//" + "download complete");
super.onPostExecute(result);
}
}
我需要而不是下载图片来下载mp3音频文件并使用我的通知
这是我使用一些旗帜并使用原始意大利面中的音频的地方。
noti.sound = Uri.parse("android.resource://"
+ _context.getPackageName() + "/" + R.raw.galo);
noti.flags |= Notification.FLAG_NO_CLEAR;
noti.defaults |= Notification.DEFAULT_LIGHTS;
noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
noti.priority = Notification.PRIORITY_MAX;
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, noti);
这里我需要使用相同的类来下载音频,但不知道我该怎么做。下载音频文件,让SDCARD无可争议。
noti.sound = Uri.parse("android.resource://" + _context.getPackageName() + "/" + R.raw.galo);
答案 0 :(得分:0)
您可以尝试使用此类下载文件
//来电话 新的DownloadFileFromURL()。执行(&#34; download_url&#34;);
//下载文件的类
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url)
{
int count;
try
{
//file name with extension
File file=new File("/directory/file.mp3");
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
pDialog.dismiss();
}
}