我正在製作一個應用程序,可以從攝像頭拍攝圖片並將其加載到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中運行良好,但在三星中沒有。
您的活動如何處理配置更改? – QuickNick
看看這篇文章:http://stackoverflow.com/questions/4901752/android-2-2-sdk-droid-x-camera-activity-doesnt-finish-properly/8679892#8679892我解決了關於這個問題使用醜陋的解決方法調用onCreate函數。 – Neonigma