2011-11-28 61 views
3

我正在製作一個應用程序,可以從攝像頭拍攝圖片並將其加載到ImageView中。 現在我的問題是,當我打開一個攝像頭,拍照並返回到我的應用程序,然後 onCreate方法也被調用,然後調用onActivityResult後。當從攝像頭返回結果時也調用onCreate方法

這個問題出現在三星的模型中。當我在HTC Wildfire上運行這段代碼時,一切正常。在HTC Wildfire onActivityResult被調用,然後onResume被調用。所以我想要在HTC Wildfire中實現 ,但不能三星S.

這裏是我的代碼,

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     Drawable drawable = null; 
     System.out.println("Faces Fond"+facesFound); 
     System.out.println("On Activity Result is called"); 
     switch (requestCode) { 

     case Constants.PICK_IMAGE: 
      switch(resultCode) 
      { 
      case RESULT_CANCELED: 
       break; 
      case RESULT_OK: 
       dragLayer.removeAllViews(); 
       isFaceDetected = false; 
       resetArray(); 
       numberOfViews = 0; 
       if(data!=null){ 
       uriOfImage = data.getData(); 
       System.out.println("Uri is "+ uriOfImage); 
       if(bitmapForDragLayer != null) 
       bitmapForDragLayer.recycle(); 
        try { 
         bitmapForDragLayer = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriOfImage)); 
         bitmapForDragLayer = Bitmap.createScaledBitmap(bitmapForDragLayer, widthOfBitmap, heightOfBitmap, true); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
        } 
        drawable = new BitmapDrawable(bitmapForDragLayer); 
        dragLayer.setBackgroundDrawable(drawable); 
        isImageLoaded = false; 
        } 
       break; 
      } 
       break; 

     case Constants.TAKE_PICTURE_CODE: 
      switch(resultCode) 
      { 
      case RESULT_CANCELED: 
       break; 
      case RESULT_OK: 
       dragLayer.removeAllViews(); 
       isFaceDetected = false; 
       resetArray(); 
       numberOfViews = 0; 
       System.out.println("Value of data"+data); 
       if(data!=null){ 
       bitmapForDragLayer = (Bitmap)data.getExtras().get("data"); 
       bitmapForDragLayer = Bitmap.createScaledBitmap(bitmapForDragLayer, widthOfBitmap, heightOfBitmap, true); 
        drawable = new BitmapDrawable(bitmapForDragLayer); 
       dragLayer.setBackgroundDrawable(drawable); 
       isImageLoaded = false; 
       } 
       break; 
      } 
      break; 

     case Constants.FRAME_SELECT_CODE: 
      currentFrame = frameSelection.getFrameBitmap(resultCode); 
      currentSelectedBitmapVO.setSelectedBitmap(selectedFrame); 
      selectedFrame = currentFrame; 
        for(int i=0;i<array.size();i++) 
        { 
         System.out.println("value of i"+i); 
         if(array.get(i)) 
         { 
         int height = currentBitmapVO[i].getSelectedBitmapHeight(); 
         int width = currentBitmapVO[i].getSelectedBitmapWidth(); 
         Bitmap b = Bitmap.createScaledBitmap(currentFrame, width, height, true); 
         selectedFrame = b; 
         imageView[i].setImageBitmap(selectedFrame); 
         imageView[i].invalidate(); 
         } 
        } 
       break; 
     } 
} 

這裏是調用相機,可拍攝圖片的代碼。

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, Constants.TAKE_PICTURE_CODE); 

相同的代碼在HTC中運行良好,但在三星中沒有。

+0

您的活動如何處理配置更改? – QuickNick

+0

看看這篇文章:http://stackoverflow.com/questions/4901752/android-2-2-sdk-droid-x-camera-activity-doesnt-finish-properly/8679892#8679892我解決了關於這個問題使用醜陋的解決方法調用onCreate函數。 – Neonigma

回答

1

也許你的應用程序已被系統殺死,因爲拍照時內存不足。然後,活動生命週期將再次開始:onCreate()

+0

但是這個代碼在HTC Wildfire中工作正常,它具有較低的處理器,三星S擁有比HTC Wildfire更高的處理器。 – Pankit

+0

我檢查logCat,我發現onCreate和onResume被調用兩次,onActivityResult被調用一次。順序如下。 1.onCreate 2.onActivityResult 3.onResume 4.onCreate 5.onResume。 – Pankit

+0

@Pankit,我解決了同樣的問題。這是你正在尋找的: http://stackoverflow.com/questions/10411009/activity-killed-oncreate-called-after-taking-picture-via-intent – Cookienator

相關問題