2012-11-09 25 views
1

拍攝的照片在我的應用程序後,當我把相機意圖通過:安卓:無法調用onActivityResult()方法,通過相機

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

它不叫onActivityResult()方法。

在我的應用程序中的問題是,有一段時間它調用此方法,但捕獲照片後的一段時間它再次來到照片捕獲屏幕。

在拍攝照片之前,我在onSaveInstanceState()之後保存了很多數據,之後我通過onRestoreInstanceState()收集了這些數據。

這裏我不知道爲什麼有時候會調用onActivityResult()方法,但是有一段時間這個方法在進入拍照模式後不會調用。

onActivityResult()代碼:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
    String fileName = null; 
    try { 
     if (requestCode == CAMERA_REQUEST) { 
      roughBitmap = (Bitmap) data.getExtras().get("data"); 
      final ContentResolver cr = getContentResolver(); 
      final String[] p1 = new String[] { 
       MediaStore.Images.ImageColumns._ID, 
        MediaStore.Images.ImageColumns.DATE_TAKEN }; 
      Cursor c1 = cr.query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, 
         null, p1[1] + " DESC"); 
       if (c1.moveToFirst()) { 
        String uristringpic = "content://media/external/images/media/" 
          + c1.getInt(0); 
        newuri = Uri.parse(uristringpic); 
        // Log.i("TAG", "newuri "+newuri); 
        snapName = getRealPathFromURI(newuri); 
        Uri u = Uri.parse(snapName); 

        File f = new File("" + u); 
        fileName = f.getName(); 
       } 
       c1.close(); 

        setImageParameter(); 

      } 
     } catch (NullPointerException e) { 

     } 
     System.out.println("*** End of onActivityResult() ***"); 
    } 

public void setImageParameter() { 
    // decode full image 
    roughBitmap = BitmapFactory.decodeFile(snapName); 
    // calc exact destination size 
    Matrix matrix = new Matrix(); 
    RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), 
      roughBitmap.getHeight()); 
    // RectF outRect = new RectF(0, 0, dstWidth, dstHeight); 
    RectF outRect = new RectF(0, 0, 640, 480); 
    matrix.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); 
    float[] values = new float[9]; 
    matrix.getValues(values); 
    // resize bitmap 
    resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, 
      (int) (roughBitmap.getWidth() * values[0]), 
      (int) (roughBitmap.getHeight() * values[4]), true); 

    paint = new Paint(); 
    paint.setColor(Color.GREEN); 
    paint.setTextSize(16); 
    paint.setTextAlign(Paint.Align.LEFT); 

    Canvas canvas = new Canvas(resizedBitmap); 

    canvas.drawText(String.valueOf(lat), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 50, paint); 
    canvas.drawText(String.valueOf(lng), resizedBitmap.getWidth() - 140, resizedBitmap.getHeight() - 50, paint); 
    if (!editTextRoadName.getText().toString().equalsIgnoreCase("")) { 
     canvas.drawText(editTextRoadName.getText().toString(), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 30, paint); 
    } 
    canvas.drawText(new DateClass().getSysDateTimeForPhoto(), resizedBitmap.getWidth() - 290, resizedBitmap.getHeight() - 10, paint); 



    if (nFinalOrientation == 1) { 
     matrix.postRotate(90); 
    } else { 
     matrix.postRotate(0); 
    } 


    rotatedBitmap = Bitmap.createScaledBitmap(resizedBitmap, 
      (int) (resizedBitmap.getWidth() * values[0]), 
      (int) (resizedBitmap.getHeight() * values[4]), true); 

    if (booleanPhotoFlag) { 
     booleanPhotoFlag = false; 
     photoBitmap = rotatedBitmap.copy(Bitmap.Config.ARGB_8888, true); 
     imageViewPhoto.setImageBitmap(photoBitmap); 

    } else { 
     landmarkBitmap = rotatedBitmap.copy(rotatedBitmap.getConfig(), rotatedBitmap.isMutable() ? true : false); 
     imageViewLocationPhoto.setImageBitmap(landmarkBitmap); 
    } 

} 
+0

我在 之前回答了這個問題 看一看: http:// stackoverflow。com/a/30266776/1531683 –

回答

3

我創建了一個Camera Library Project你可以用它來拍照。我建議你使用這個庫進行拍照

編輯2:

看試着將protected void onActivityResult(int requestCode, int resultCode, Intent data)super.onActivityResult(requestCode, resultCode, data);之間的日誌,看看如果連這個方法被調用與否,如果它被稱爲然後檢查返回的值。 AND onActivityResult不是由您的活動調用的,而是由調用活動在這種情況下爲intentMediaStore.ACTION_IMAGE_CAPTURE,因此您的onActivityResult必須由調用活動的setResult()調用。但我希望你不是想通過意圖通過位圖like here

+0

鏈接不好回答;沒有圖書館;特別是對於一個不那麼複雜的任務 –

+0

@MoJo鏈接是我創建的用於相機處理庫的項目。爲什麼要在這裏粘貼代碼,當我能夠顯示正確的路徑時,首先至少考慮閱讀它或在投票前訪問鏈接 –

+0

我知道你已經給了一個圖書館;但你認爲圖書館需要執行維克什希望的行動嗎?世界已經很複雜,不會讓它變得更加複雜;如果你能夠編寫一個庫,那麼你應該能夠選擇他的代碼來執行這個任務;多數民衆贊成在他想要的 –

0

您可以通過創建這樣

Intent cameraIntent = new Intent(
         android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, 0); 

意圖和onActivityResult這樣

public void onActivityResult(int intRequestCode, int intResultCode, 
      Intent objresult) { 
     if (intResultCode == RESULT_OK) { 
     if (intRequestCode == 0) { 
      try { 
       Bitmap photo = (Bitmap) objresult.getExtras().get("data"); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       photo.compress(Bitmap.CompressFormat.PNG, 100, stream); 
       byte[] byteArray = stream.toByteArray(); 
       strAttachmentCoded = new String(byteArray); 
       } catch (Exception e) { 
       Toast.makeText(context, "Picture not stored. Please try again", 
         Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

:)

實現這一目標
+0

我嘗試這個代碼,但直到問題出現。 –

+0

是否在設備或模擬器上測試? –

+0

我正在測試設備... –

-1
I faced same problem , once check have you put any tag related to History ? 
Do not put android:noHistory="true" tag in manifest 
if you use android:noHistory="true" in your activity inside manifest , it will 
remove from stack after on stop . 

注意:如果您使用選項卡活動,那麼您也應該n不要使用android:noHistory =「true」 ,否則只需在清單中的activity中放置android:noHistory =「false」。

1

檢查是否有權限在AndroidManifest.xml

<uses-feature android:name="android.hardware.camera" android:required="false"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

的WRITE_EXTERNAL_STORAGE是挺大bigmap,你必須存儲在磁盤映像文件首盤。

1

我在manifest.xml中的活動中使用了android:launchMode="singleInstance"。刪除它,它又開始工作。希望能幫助到你。