3

當用戶單擊十字架以不接受照片時,它將以與接受照片時相同的方式結束該意圖。它將文件保存到設備庫。但它是空白的。不應該點擊交叉表示resultCode!= RESULT_OK?是否還有一個檢查我缺少?謝謝。這是代碼。等等,我在活動結果之前保存圖像......這是一個有缺陷的系統,但它在官方的Android Developers網站上。如果有人可以提出修復建議,我會非常感激,因爲我曾經將圖像保存在onActivtyResult中,並且它在某些手機上不起作用,導致出現異常,所以我改爲這樣。即使用戶未接受照片,相機意圖onActivityResult代碼也會保存(空白)圖像

要啓動的意圖:內onActivityResult

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

private File createImageFile() throws IOException { 
       // Create an image file name 
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
       String imageFileName = "JPEG_" + timeStamp + "_"; 
       File storageDir = Environment.getExternalStoragePublicDirectory(
         Environment.DIRECTORY_PICTURES); 
       File image = File.createTempFile(
        imageFileName, /* prefix */ 
        ".jpg",   /* suffix */ 
        storageDir  /* directory */ 
      ); 

       // Save a file: path for use with ACTION_VIEW intents 
       mCurrentPhotoPath = image.getAbsolutePath(); 
       ih.galleryAddPic(mCurrentPhotoPath, this.getApplicationContext()); 
       return image; 
      } 

相機意圖的情況:

else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == RESULT_OK)){ 
               mProfilePicPath = mCurrentPhotoPath; 
               mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath, 
                 GlobalConstants.PROFILE_PICTURE_RESOLUTION, 
                 GlobalConstants.PROFILE_PICTURE_RESOLUTION); 
               TextView tv = (TextView) findViewById(id.ProfilePicText); 
           tv.setText(mProfilePicPath); 
          } 
        }catch(Exception ex){ 
          Log.d("shkdghrfb", ex.toString()); 
        } 
      } 

編輯:我改變onActivityResult到這一點,但無濟於事(空白圖像仍在我的畫廊之後,刪除的值爲true):

else if (requestCode == REQUEST_TAKE_PHOTO){ 
          if(resultcode == RESULT_OK){ 
           File f = new File(mCurrentPhotoPath); 
           mProfilePicPath = null; 
           if (f.exists()) { 
            if (f.length() != 0){ 
              mProfilePicPath = mCurrentPhotoPath; 
              mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath, 
                GlobalConstants.PROFILE_PICTURE_RESOLUTION, 
                GlobalConstants.PROFILE_PICTURE_RESOLUTION); 
              TextView tv = (TextView) findViewById(id.ProfilePicText); 
              tv.setText(mProfilePicPath); 
            } 
            else { 
            boolean deleted = f.delete(); 
            if (deleted == true){ 
            Log.d("camera0", "deleted"); 
            } 
            else{ 
             Log.d("camera0", "not deleted"); 
            } 
           } 
          } 
         } 
         else{ 
          File f = new File(mCurrentPhotoPath); 
          boolean deleted = f.delete(); 
          if (deleted == true){ 
          Log.d("camera", "deleted"); 
          } 
          else{ 
           Log.d("camera", "not deleted"); 
          } 
         } 
        } 
      }catch(Exception ex){ 
        Log.d("shkdghrfb", ex.toString()); 
      } 
       }catch(Exception ex){ 
         Log.d("shkdghrfb", ex.toString()); 
       } 

編輯好的我相信我需要在刪除後用MediaScannerIntent掃描SD卡的相應區域,以便顯示,因爲它看起來現在可以工作。

回答

2

你不是用createImageFile()創建文件嗎? 您可以保存photoFile和結果!= RESULT_OK刪除

順便說一句,攝像頭的應用程序(甚至默認)可能會返回錯誤的結果。在日誌中檢查它。如果他們這樣做,只是不要依賴結果&檢查創建的文件的大小。如果== 0 - 刪除它

+0

謝謝,聽起來不錯。明天會放棄它。有,或者只保存onActivityResult,這可能會減少應用程序的工作,儘管我懷疑它會造成任何性能差異。 – user3164083

+0

我試過了,空白圖片仍在畫廊中。我把我的嘗試置於我的問題的底部。乾杯。請注意,mCurrentPhotoPath被設置爲'createImageFile()'中的file.getAbsolutePath' – user3164083

+0

它現在可用。也許只是添加到答案中,不要忘記在刪除後用MediaScannerIntent掃描SD卡的相應區域,否則它不會顯示刪除,這是令人困惑的。謝謝! – user3164083

相關問題