3
同時插入到表正在逐漸嘗試重新打開已關閉的對象異常越來越試圖重新打開已關閉的對象
public class UploadImagesToServer {
static ImgeURLdb imgURLdb;
static Context mContext;
public static void postImage(String ImageLink, String imageName, Context context) {
mContext = context;
imgURLdb = new ImgeURLdb(mContext);
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("file",new File(ImageLink));
Log.i("imageName","Sending Image"+ImageLink);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Constants.MediaUpload, jsonObject, future, future);
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(request);
try {
JSONObject response = future.get(10, TimeUnit.SECONDS);// Blocks for at most 10 seconds.
String imageURL = response.getString("");
addImagetoDB(imageName,imageURL);
Log.i("imageName","getting URL"+imageURL+"");
} catch (InterruptedException e) {
// Exception handling
} catch (ExecutionException e) {
// Exception handling
} catch (TimeoutException e) {
//addImagetoDB(imageName,"imageURL");
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
private static void addImagetoDB(String imageName, String imageURL) {
imgURLdb.addImageURL(imageName,imageURL);
}
}
上面的代碼添加URL到DB
public void addImageURL(String imageName, String imageURL){
DbHelper mDBhelper = new DbHelper(mContext);
SQLiteDatabase sqlDB = mDBhelper.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "Select id from "+DbHelper.IMAGE_URL+" where "+DbHelper.IMAGE_NAME + "=?";
String[] args = new String[1];
args[0] = imageName;
Cursor myCursur = mDBhelper.myquery(query,args);
if (myCursur != null && myCursur.getCount() > 0) {
myCursur.moveToPosition(-1);
while (myCursur.moveToNext()) {
values.put(DbHelper.IMAGE_URL,imageURL);
sqlDB.insert(DbHelper.IMAGE_URL, null, values);
}
myCursur.close();
} else
{
Log.e("tag", "Cursor is zero");
}
mDBhelper.close();
sqlDB.close();
}
這是將圖像URL添加到數據庫的方法。雖然添加嘗試重新打開已關閉的對象異常
請發佈DbHelper類的代碼,有一些問題。最可能在getWritableDatabase()方法或myquery()方法中。 –