1使用xutils3進行下載
2先實現下載一張圖片到本地
3使用for循環實現多張圖片
/**
* 文件下載地址
*/
String downloadUrl = "";
/**
* 下載文件的保存位置
*/
String saveFilePath = "";
//點擊事件
mUpDataBt.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("圖片更新中");
dialog.show();
listImage.clear();
listImage = dbUtil.findPicName();
// 沒有skuimg文件夾則創建skuimg
String sdPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/skuimg";
File file = new File(sdPath);
if (!file.exists()) {
file.mkdirs();
Log.e("wy", "創建了skuimg文件夾");
}
new Thread() {
@Override
public void run() {
super.run();
for (int i = 0; i < listImage.size(); i++) {
Object imageString = listImage.get(i);
if (imageString != null) {
downloadUrl = Constant.BASE_URL + "/" + imageString;
saveFilePath = getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/" + imageString;
Log.e("wy", "saveFilePath: " + saveFilePath);
DownLoadUtil.DownLoadFile(downloadUrl, saveFilePath, new MyCallBack<File>() {
@Override
public void onSuccess(File result) {
super.onSuccess(result);
// DownLoadUtil.showToast(MainActivity.this, "下載成功");
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback);
dialog.dismiss();
Log.e("wy", "圖片下載失敗原因: " + ex.getMessage());
// DownLoadUtil.showToast(MainActivity.this, "下載失敗");
}
});
}
}
Log.e("wy", "run: " + "下載圖片成功");
dialog.dismiss();
}
}.start();
return true;
}
});
package com.wintec.huashang.utils;
import android.content.Context;
import android.widget.Toast;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
public class DownLoadUtil {
/**
* 下載文件
*/
public static <T> Callback.Cancelable DownLoadFile(String url, String filepath, Callback.CommonCallback<T> callback) {
RequestParams params = new RequestParams(url);
//設置斷點續傳
params.setAutoResume(true);
params.setSaveFilePath(filepath);
Callback.Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}
/**
* 單例吐司
*/
private static Toast toast;
public static void showToast(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
}
toast.setText(msg);
toast.show();
}
}
package com.wintec.huashang.utils;
import org.xutils.common.Callback;
public class MyCallBack<ResultType> implements Callback.CommonCallback<ResultType> {
@Override
public void onSuccess(ResultType result) {
//根據需求進行請求成功的邏輯處理
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//根據需求進行請求網絡失敗的邏輯處理
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
}
查表中某一列的全部數據的sql語句
public ArrayList findPicName() {
ArrayList goodsList = new ArrayList();
// Cursor cursor = mSQLiteDatabase.rawQuery("select name from Person where searchKey like ? ", new String[]{"'%"+searchKey+"%'"});
Cursor cursor = mSQLiteDatabase.rawQuery("select previewImage from Person ", new String[]{});
int count = cursor.getCount();
Log.e("wy", "名稱同類的數量: " + count);
for (int i = 0; i < count; i++) {
while (cursor.moveToNext()) {
// Log.e("wy","獲取到分類商品: "+ cursor.getString(i));
String goods = cursor.getString(i);
goodsList.add(goods);
}
}
Log.e("wy", "集合大小爲: " + goodsList.size());
cursor.close();
return goodsList;
}