2013-08-07 66 views
2

我正在使用以下代碼來刪除圖像。它的工作原理是第一次,但是當我試圖捕捉到的圖像,並刪除它 我得到了StaleDataException試圖在光標關閉後嘗試訪問光標

08-07 14:57:24.156: E/AndroidRuntime(789): java.lang.RuntimeException: Unable to    
     resume activity {com.example.cap_im/com.example.cap_im.MainActivity}: 
     android.database.StaleDataException: Attempted to access a cursor after it has been closed. 

public void deleteImageFromGallery(String captureimageid) { 
    Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

    getContentResolver().delete(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      BaseColumns._ID + "=?", new String[] { captureimageid }); 

    String[] projection = { MediaStore.Images.ImageColumns.SIZE, 
      MediaStore.Images.ImageColumns.DISPLAY_NAME, 
      MediaStore.Images.ImageColumns.DATA, BaseColumns._ID, }; 

    Log.i("InfoLog", "on activityresult Uri u " + u.toString()); 

    try { 
     if (u != null) { 
      cursor = managedQuery(u, projection, null, null, null); 
     } 
     if ((cursor != null) && (cursor.moveToLast())) { 

      int i = getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        BaseColumns._ID + "=" + cursor.getString(3), null); 
      Log.v(TAG, "Number of column deleted : " + i); 
     } 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 
+0

所以你的問題到底是什麼?該錯誤幾乎可以解釋自己 – tyczj

+0

秒tme我無法刪除圖像 – Dilshi

+0

該錯誤指向什麼行? – thegrinner

回答

6

在你finally塊,你關閉遊標,但你不知道將其設置爲null。因此,下次您的方法被調用,cursor.getString(3)失敗,因爲遊標已關閉。

解決方法:在您的finally區塊中將cursor設置爲null

正確的解決方案:不要爲您的cursor使用實例變量,而是在您的方法中使用局部變量。

33

函數,managedQuery()已棄用。

請使用getContentResolver()。query()。

參數相同。

+1

+1用於清理和快速解決方案 – alp

+1

這應該是標記的答案。 – Searock

+0

解決了這個問題,太好了,謝謝! – TSGames

2

我有一個類似的問題。在我的情況下,代碼是好的,但我用的方法已過時「managedQuery」,而不是一個由湯姆·林

public String getPath(Uri uri) { 
    if (uri == null) { 
     return null; 
    } 
    String result = null; 

    String[] projection = {MediaStore.Images.Media.DATA}; 
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 

    // deprecated: 
    // Cursor cursor = managedQuery(uri, projection, null, null, null); 

    if (cursor != null) { 

     int columnIndex = 0; 
     try { 
      columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      result = cursor.getString(columnIndex); 
     } catch (IllegalArgumentException e) { 
      Log.e("While getting path for file", e); 
     } finally { 
      try { 
       if (!cursor.isClosed()) { 
        cursor.close(); 
       } 
       cursor = null; 
      } catch (Exception e) { 
       Log.e("While closing cursor", e); 
      } 
     } 
    } 
    return result; 
} 

建議如果我使用,如果我省略了最後它仍然有效的方法已過時。在doc中也明確指出,在使用此方法時不應關閉遊標

1

也不要使用​​它已被棄用。

除去此:

protected SQLiteDatabase database; 

,並使其局部

基本上2方法被同時執行和一個方法叫做database.close()和第二方法仍在訪問數據,所以Exception

使用這樣的:

public class db { 

    DataBaseHelper dbHelper; 
    Context mContext; 


    public db(Context context) { 
     this.mContext = context; 
    } 

    public db open() throws SQLException { 
     dbHelper = new DataBaseHelper(mContext); 
     return this; 
    } 


    public void close() { 
     dbHelper.close(); 
    } 

    public void insertdb(int id,String ph_num, String call_type, String calldate, String call_duration, String upload_status) { 
     SQLiteDatabase database = dbHelper.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(DataBaseHelper.id,id); 
     values.put(DataBaseHelper.phone_number, ph_num); 
     values.put(DataBaseHelper.call_type, call_type); 
     values.put(DataBaseHelper.call_date, calldate); 
     values.put(DataBaseHelper.call_duration, call_duration); 
     values.put(DataBaseHelper.upload_status, upload_status); 
     database.insert(DataBaseHelper.table_name, null, values); 
     database.close(); 
     // Log.d("Database helper", "values inserted"); 
    } 


    public ArrayList<HashMap<String, String>> getAllUsers() { 
     ArrayList<HashMap<String, String>> wordList; 
     wordList = new ArrayList<HashMap<String, String>>(); 
     String selectQuery = "SELECT * FROM call_logtable"; 
     SQLiteDatabase database = dbHelper.getWritableDatabase(); 
     Cursor cursor = database.rawQuery(selectQuery, null); 
     if (cursor.moveToFirst()) { 
      do { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("id", cursor.getString(0)); 
       map.put("phone_number", cursor.getString(1)); 
       map.put("call_type", cursor.getString(2)); 
       map.put("call_date", cursor.getString(3)); 
       map.put("call_duration", cursor.getString(4)); 
       wordList.add(map); 
      } while (cursor.moveToNext()); 
     } 
     cursor.close(); // just added 
     database.close(); 
     return wordList; 
    } 

    /** 
    * Compose JSON out of SQLite records 
    * @return 
    */ 
    public String composeJSONfromSQLite(){ 
     ArrayList<HashMap<String, String>> wordList; 
     wordList = new ArrayList<HashMap<String, String>>(); 
     String selectQuery = "SELECT * FROM call_logtable where upload_status = '"+"no"+"'"; 
     SQLiteDatabase database = dbHelper.getWritableDatabase(); 
     Cursor cursor = database.rawQuery(selectQuery, null); 
     if (cursor.moveToFirst()) { 
      do { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("id", cursor.getString(0)); 
       map.put("phone_number", cursor.getString(1)); 
       map.put("call_type", cursor.getString(2)); 
       map.put("call_date", cursor.getString(3)); 
       map.put("call_duration", cursor.getString(4)); 
       wordList.add(map); 
      } while (cursor.moveToNext()); 
     } 
     cursor.close(); // just added 
     database.close(); 
     Gson gson = new GsonBuilder().create(); 
     //Use GSON to serialize Array List to JSON 
     return gson.toJson(wordList); 
    } 


    public int dbSyncCount(){ 
     int count = 0; 
     String selectQuery = "SELECT * FROM call_logtable where upload_status = '"+"no"+"'"; 
     SQLiteDatabase database = dbHelper.getWritableDatabase(); 
     Cursor cursor = database.rawQuery(selectQuery, null); 
     count = cursor.getCount(); 
     cursor.close(); // just added 
     database.close(); 
     return count; 
    } 


    public void updateSyncStatus(String id, String status){ 
     SQLiteDatabase database = dbHelper.getWritableDatabase(); 
     String updateQuery = "Update call_logtable set upload_status = '"+ status +"' where id="+"'"+ id +"'"; 
     Log.d("query", updateQuery); 
     database.execSQL(updateQuery); 
     database.close(); 
    } 

    public Cursor getinformation() 
    { 
     SQLiteDatabase database = dbHelper.getReadableDatabase(); 
     String[] columns={DataBaseHelper.phone_number,DataBaseHelper.call_type,DataBaseHelper.call_date,DataBaseHelper.call_duration,DataBaseHelper.upload_status}; 
     return database.query(DataBaseHelper.table_name,columns,null,null,null,null,null); 
    } 

    public void delete() 
    { 
     SQLiteDatabase database = dbHelper.getWritableDatabase(); 
     // String[] columns={DataBaseHelper.phone_number,DataBaseHelper.call_type,DataBaseHelper.call_date,DataBaseHelper.call_duration}; 
     database.delete(DataBaseHelper.table_name, null, null); 
    } 


    StringBuffer readSpecificfrom_db(String type) 
    { 
     String ph_number=null; 
     String call_type=null; 
     String call_date=null; 
     String call_duration=null; 
     String upload_status=null; 
     StringBuffer sb = new StringBuffer(); 
     //sb.append("Call Log :"); 
     Cursor cursor_object=getinformation(); 
     cursor_object.moveToFirst(); 
     do { 
      if((cursor_object.getString(1)).equals(type)) { 
       ph_number = cursor_object.getString(0); 
       call_type = cursor_object.getString(1); 
       call_date = cursor_object.getString(2); 
       call_duration = cursor_object.getString(3); 
       if(type=="Missed") { 
        sb.append("\nPhone Number:--- " + ph_number + 
            " \nCall Type:--- " + call_type + 
            " \nCall Date:--- " + call_date 
          // + " \nCall duration in sec :--- " + call_duration 
        ); 
        sb.append("\n----------------------------------"); 
       } 
       else 
       { 
        sb.append("\nPhone Number:--- " + ph_number + 
          " \nCall Type:--- " + call_type + 
          " \nCall Date:--- " + call_date 
          + " \nCall duration in sec :--- " + call_duration); 
        sb.append("\n----------------------------------"); 
       } 
      } 
     }while(cursor_object.moveToNext()); 
     cursor_object.close(); // just added 
     return sb; 

    } 

    StringBuffer readfrom_db() 
    { 
     String ph_number=null; 
     String call_type=null; 
     String call_date=null; 
     String call_duration=null; 
     String upload_status; 
     // int id=0; 
     StringBuffer sb = new StringBuffer(); 
     // sb.append("Call Log :"); 
     Cursor cursor_object=getinformation(); 
     cursor_object.moveToFirst(); 
     do { 
      ph_number=cursor_object.getString(0); 
      call_type=cursor_object.getString(1); 
      call_date=cursor_object.getString(2); 
      call_duration=cursor_object.getString(3); 
      sb.append("\nPhone Number:--- " + ph_number + 
        " \nCall Type:--- " + call_type + 
        " \nCall Date:--- " + call_date 
        + " \nCall duration in sec :--- " + call_duration); 
      sb.append("\n----------------------------------"); 

     } while(cursor_object.moveToNext()); 
     cursor_object.close(); // just added 
     return sb; 
    } 
} 
1

肯定會使用此代碼,使用getContentResolver()。query()而不是managedQuery()。由於managedQuery()已被棄用。其工作對我完美。謝謝你