试图重新打开已经关闭的对象

时间:2016-07-07 09:49:16

标签: android android-sqlite

在插入表时尝试重新打开已经关闭的对象异常

   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);


    }
}

上面的代码是将dbl添加到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添加到db的方法。正在添加尝试重新打开已经关闭的对象异常

1 个答案:

答案 0 :(得分:0)

我无法看到你的&#34; myquery()&#34;的执行情况。 DbHelper中的方法,但你可能关闭该方法中的光标,然后返回它。如果你不关闭那里的光标,它应该没问题。