I have this situation: two fragments inside a FragmentActivity. I have a method for shooting, and I would like to get the results from onActivityResult . The problem is that the onActivityResult method always has Activity.RESULT_CANCELED . I used to use the same methods in Activity, and they worked well. The code below (comments in Portuguese, but I think this is not a problem, because the code here is pretty simple). I saw a similar question, but no one could help me.
public class CameraActivity extends FragmentActivity implements OnClickListener { private static final String TAG = "CameraActivity"; Fragment fragmentToqueParaTirarFoto; Fragment fragmentFotoCarregada; // Constantes para abrir activity de tirar foto e escolher foto do álbum private static final int TAKE_PICTURE = 1; String caminhoDaPastaDeFotos = ""; String arquivoFullPath = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.camera, menu); return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult( requestCode, resultCode, data); if (requestCode == TAKE_PICTURE) { if (resultCode == Activity.RESULT_OK) { /*********************************** CODE NEVER REACHED THIS POINT. ***********************************/ Log.d(TAG, "Result OK"); } if (resultCode == Activity.RESULT_CANCELED) { Log.d(TAG, "Result Cancel"); } } } /** * Função que abre intent para tirar foto. É adicionado parâmetro extra */ public void takePicture() { // Intent que abre camera. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); // Novo nome do arquivo arquivoFullPath = newFileName(); String identificadorDeArquivo = "file://"; Uri outputFileUri = Uri.parse(identificadorDeArquivo + arquivoFullPath); // Adiciona nome absoluto do arquivo da foto tirada intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); // Inicia intent com camera. startActivityForResult(intent, TAKE_PICTURE); } /** * Gera nome do arquivo com o mesmo formato de nome gerado pelo Android. O * formato do nome é IMG_ + yyyyMMdd_HHmmss + .jpg onde yyyyMMdd_HHmmss é a * hora local. * * @return Retorna nome absoluto para imagem usando formato: IMG_ + * yyyyMMdd_HHmmss + .jpg */ @SuppressLint("SimpleDateFormat") private String newFileName() { // Formato de data DateFormat dateFormat = new SimpleDateFormat(getResources().getString( R.string.string_formato_de_data_para_nome_do_arquivo)); // Data atual Date date = new Date(); // Nome do arquivo String newFileName = "IMG_" + dateFormat.format(date).toString() + ".jpg"; // Nome absoluto para a nova imagem gerada. arquivoFullPath = caminhoDaPastaDeFotos + newFileName; return arquivoFullPath; } }
source share