1

我正在使用自定義相機(使用Surfaceview等)的應用程序,我從我的ObjectActivity中使用startActivityForResult轉到名爲CameraActivity的相機的活動。這發生在這種方法中。startActivityForResult返回到錯誤的活動

public void addPicture(View v) { 
    final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" }; 
    AlertDialog.Builder builder = new AlertDialog.Builder(ObjectActivity.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(items, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (items[item].equals("Take Photo")) { 
       executeOnResume = false; 
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 
        int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.CAMERA); 
        if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) { 
         requestPermissions(new String[]{Manifest.permission.CAMERA}, 
           REQUEST_CODE_ASK_PERMISSIONS); 
        } 
       } 
       Intent intent = new Intent(ObjectActivity.this,CameraActivity.class); 
       startActivityForResult(intent, REQUEST_CAMERA); 
      } else if (items[item].equals("Choose from Gallery")) { 
       executeOnResume = false; 
       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       intent.setType("image/*"); 
       startActivityForResult(
         Intent.createChooser(intent, "Select File"), 
         REQUEST_SELECT_FILE); 
      } else if (items[item].equals("Cancel")) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    builder.show(); 
} 

更具體地講,它發生在這幾行:

Intent intent = new Intent(ObjectActivity.this,CameraActivity.class); 
startActivityForResult(intent, REQUEST_CAMERA); 

這工作得很好,但是當我試圖拍照,這恰好這裏後返回ObjectActivity:

Camera.PictureCallback mPicture = new Camera.PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     //TODO Code to process picture taken 
     //create a new intent... 
     Intent intent = new Intent(); 
     intent.putExtra("data",data); 
     //close this Activity... 
     setResult(Activity.RESULT_OK, intent); 
     finish(); 
    } 
}; 

它返回到名爲MainActivity的ObjectActivity之前的活動,而它應該返回到ObjectActivity並調用onActivityResult():

@Override 
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 
    executeOnResume = false; 
    loadStuff(); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_SELECT_FILE) { 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 
      alertDialog.setTitle("Description"); 
      alertDialog.setMessage("Enter Description"); 
      final EditText input = new EditText(this); 
      alertDialog.setView(input); 
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.MATCH_PARENT); 
      input.setLayoutParams(lp); 
      alertDialog.setView(input); 
      alertDialog.setPositiveButton("Ok", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          description = input.getText().toString(); 
          if (description == null || description.equals("")) { 
           description = "-"; 
          } 
          try { 
           savePhoto(requestCode,data); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }); 
      final AlertDialog.Builder tmpDialog = alertDialog; 

      final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); 
      dlgAlert.setTitle("Direction"); 
      dlgAlert.setMessage("Stand with your phone facing the same direction as the picture made and press Ok"); 
      dlgAlert.setPositiveButton("Ok", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          mBearingProvider.updateBearing(); 
          bearing = mBearingProvider.getBearing(); 
          cardinalDirection = bearingToString(bearing); 
          Log.e("Direction", cardinalDirection + "," + bearing); 
          tmpDialog.create().show(); 
          dialog.dismiss(); 
         } 
        }); 
      dlgAlert.setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          tmpDialog.create().show(); 
         } 
        }); 
      dlgAlert.create().show(); 
     } 
    } 
} 

但它永遠不會到達那裏。 有誰知道爲什麼會發生這種情況?

+0

是否使用GroupActivity? – Dinash

+0

ObjectActivity和CameraActivity都是常規活動,MainActivity是AppCompatActivity,所以據我所知我沒有使用GroupActivity –

回答

0

我已經找到了解決辦法,如果我不與意圖送過來一個byte [](而是也許寫出來一個文件併發送該文件的路徑與意圖),代碼似乎工作和ObjectActivity打開。

部分我刪除/修改:

intent.putExtra("data",data); 

我不知道爲什麼這個作品,我想知道爲什麼,但現在我很高興它的工作原理。

1

擺脫getParent。您想要設置當前活動的結果,而不是您的父母。所以更換:

//close this Activity... 
    if (getParent() == null) { 
     setResult(Activity.RESULT_OK, intent); 
    } else { 
     getParent().setResult(Activity.RESULT_OK, intent); 
    } 

setResult(Activity.RESULT_OK, intent); 
+0

我想我已經試過了,但我會再試一次 –

+2

設置父級的結果是絕對的100%錯誤。 –

+0

這可能是真的,但這不是解決方案,我得到了同樣的結果 –

相關問題