2010-12-23 20 views
5

我正在製作一個程序,拍攝一張照片然後顯示它的縮略圖。 使用模擬器時一切順利,放棄按鈕刪除照片。 但是,在真實設備上,相機意圖將圖像保存在imageUri變量中,而另一個名爲像是我打開相機並單獨拍攝照片那樣命名的圖像。Android相機意圖創建兩個文件

private static final int CAMERA_PIC_REQUEST = 1337; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 

    //start camera 
    values = new ContentValues(); 
    values.put(MediaStore.Images.Media.TITLE, "New Picture"); 
    values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera"); 
    imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    image = (ImageView) findViewById(R.id.ImageView01); 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
    startActivityForResult(intent, CAMERA_PIC_REQUEST); 


    //save the image buttons 
    Button save = (Button) findViewById(R.id.Button01); 
    Button close = (Button) findViewById(R.id.Button02); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) { 
     try{ 
      thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri); 
      image.setImageBitmap(thumbnail); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    else{ 
     finish(); 
    } 
} 

public void myClickHandler(View view) { 
    switch (view.getId()) { 
    case R.id.Button01: 
     finish(); 
     break; 
    case R.id.Button02: 
     dicard(); 
    } 
} 

private void dicard(){ 
    getContentResolver().delete(imageUri, null, null); 
    finish(); 
} 
+0

我測試了HTC Incredible上的程序,它工作得很好。這是LG ALLY手機的一個已知問題嗎? – 2010-12-25 07:24:11

回答

1

有些Android手機將原始照片存儲在畫廊中,僅在您的位置存儲縮略圖。無論您如何處理原始請求,都無關緊要。我有兩種不同的HTC手機,還有一些其他品牌沒有這樣做。

我以另一種方式解決了這個問題。我運行了庫中每個項目的查詢並將BucketIDs加載到一個數組中。我在應用程序啓動相機應用程序時執行此操作。當相機應用程序返回時,我會進行相同的查詢(最近添加了項目以節省時間)。我將它與我的原始列表進行比較,並找到新的BucketID。接下來,我將該圖像的大小與我明確設置爲輸出的文件進行比較。如果它更大,我會複製它,取代我的。然後我刪除該文件並將其從圖庫中刪除。

痛苦在你知道什麼!

[編輯]我不得不再次改變這一點,當我發現了一個手機,沒有保持唯一的桶ID ...請參閱我的帖子後面的答案更多。

+0

如果您在http://stackoverflow.com/questions/6390163/deleting-a-gallery-image-after-camera-intent-photo-taken上發佈該示例代碼的答案,那麼您將獲得500點獎勵。 – 2011-06-26 09:14:03