插入表时尝试重新打开一个已经关闭的对象异常
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 中看不到您的“myquery()”方法的实现,但您可能在该方法内关闭了游标,然后将其返回。如果您不在那里关闭光标,应该没问题。
关于android - 尝试重新打开一个已经关闭的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38242388/