我正在尝试使用jsonObject下载图像和音频 但问题是当我在后台关闭我的应用程序时效果不佳
code
public class downloadData extends AsyncTask<JSONObject, Void, ArrayList<modelCBlips>> {
Context context;
String cId;
Utils utils;
private DownloadManager mgr = null;
public downloadData(Context context, String cId) {
this.context = context;
this.cId = cId;
utils = new Utils(context);
mgr = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
context.registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
@Override
protected ArrayList<modelCBlips> doInBackground(JSONObject... params) {
ArrayList<modelCBlips> list = new ArrayList<>();
try {
JSONArray blipArray = params[0].getJSONArray("Blips");
for (int i = 0; i < blipArray.length(); i++) {
modelCBlips mcblips = new modelCBlips();
JSONObject jObjBlip = blipArray.getJSONObject(i);
String imagePath = jObjBlip.getString("imagePath");
String audioPath = jObjBlip.getString("audioPath");
mcblips.setImagePath(SaveFile(imagePath));
mcblips.setAudioPath(SaveFile(audioPath));
list.add(mcblips);
if (isCancelled())
System.out.println("istrue");
else
System.out.println("not");
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
@Override
protected void onPreExecute() {
utils.editor.putString("status" + cId, "Downloading...").commit();
}
@Override
protected void onPostExecute(ArrayList<modelCBlips> list) {
for (int i = 0; i < list.size(); i++) {
modelCBlips mcb = list.get(i);
utils.con.insertCBlips(mcb.getImagePath(), mcb.getAudioPath());
}
utils.editor.putString("status" + cId, "Downloaded").commit();
utils.editor.putBoolean("download" + cId, true).commit();
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
}
};
public String SaveFile(String path) {
String startdownloadurl = null;
try {
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("channel" + cId, Context.MODE_PRIVATE);
if (!directory.exists()) {
directory.mkdir();
}
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
/* Uri downloadUri = Uri.parse(path);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
String imgnm = path.substring(path.lastIndexOf("/") + 1);
startdownloadurl = directory + "/";
System.out.println(" directory " + startdownloadurl);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir(startdownloadurl, imgnm);
mgr.enqueue(request);
startdownloadurl = directory + "/" + imgnm;
*/
URL url = new URL(path);
String imgnm = path.substring(path.lastIndexOf("/") + 1);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream(), 2048);
startdownloadurl = directory + "/" + imgnm;
FileOutputStream output = new FileOutputStream(startdownloadurl);
byte data[] = new byte[2048];
int bytesRead = 0;
while ((bytesRead = input.read(data, 0, data.length)) >= 0) {
output.write(data, 0, bytesRead);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
System.out.println("Exception " + e);
}
return startdownloadurl;
}
}
使用此代码我尝试下载图像和音频文件,当所有图像和音频文件成功下载后,我将尝试插入我的数据库
答案 0 :(得分:0)
尝试在服务运行时在服务中运行它,即使应用已关闭