2015-03-19 29 views
0

我是從以下的Android教程如下official link 我希望保存完整的圖像。問題是,當用戶進入相機應用程序拍攝照片,但決定取消它,android仍然保存大小爲零的圖像。任何方式來避免這種情況?從相機應用程序保存的大小爲零的位圖取消

的代碼如下

static final int REQUEST_TAKE_PHOTO = 1; 

private void dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 
      ... 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        Uri.fromFile(photoFile)); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
} 

回答

0

好的解決它使用新創建的文件的名稱。當我們創建文件的名稱,我們只需保存文件的路徑在一個字符串變量表示「當前路徑」 並添加如下代碼 -

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode == Activity.RESULT_CANCELED && requestCode == 0) 
     { 
      File file = new File(currentPath); 
      boolean delete = file.delete(); 


       Log.i("delete",String.valueOf(delete)); 
     } 
    } 
0

我認爲你正在做的事情錯在你的代碼。您應該發佈您的代碼以獲得適當的幫助。據我所知,

。你應該重寫onbackpressed函數。你應該在這裏關閉/釋放相機。在這種情況下,我認爲相機不會拍攝並保存照片。

相關問題