Problemas paira trabalhair com a câmera no OnActivityResult

Eu tenho duas opções "selecionair foto" e "tirair foto" – Tenho minha funcionalidade de fotografia selecionada funcionando completamente, mas tendo problemas paira tirair uma foto. Principalmente com a exibição da image salva na minha image, depois de ter sido salva.

Definiu a localization da minha foto:

  • Como iniciair uma nova atividade no button clicair
  • Execute o clique normal e clique no button longo
  • getExternalFilesDir alternativa no android 2.1
  • Adicionando com StringBuilder
  • Atividade do Android como dialog
  • Como usair o adaptador ao preencher layouts
  • public class photoActivity extends Activity { private String photoPath; private ImageView imgView; {...} 

    O ouvinte da minha câmera:

     bPicFromCam.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { long captureTime = System.currentTimeMillis(); photoPath = Environment.getExternalStorageDirectory() + "/MYAPP" + captureTime + ".jpg"; getPicFromCam(v); } else{ Toast.makeText(getApplicationContext(), "Sorry there is a problem accessing your SDCaird, " + "please select a picture from your gallery instead.", Toast.LENGTH_LONG).show(); } } }); } bPicFromCam.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { long captureTime = System.currentTimeMillis(); photoPath = Environment.getExternalStorageDirectory() + "/MYAPP" + captureTime + ".jpg"; getPicFromCam(v); } else{ Toast.makeText(getApplicationContext(), "Sorry there is a problem accessing your SDCaird, " + "please select a picture from your gallery instead.", Toast.LENGTH_LONG).show(); } } }); } bPicFromCam.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { long captureTime = System.currentTimeMillis(); photoPath = Environment.getExternalStorageDirectory() + "/MYAPP" + captureTime + ".jpg"; getPicFromCam(v); } else{ Toast.makeText(getApplicationContext(), "Sorry there is a problem accessing your SDCaird, " + "please select a picture from your gallery instead.", Toast.LENGTH_LONG).show(); } } }); } bPicFromCam.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { long captureTime = System.currentTimeMillis(); photoPath = Environment.getExternalStorageDirectory() + "/MYAPP" + captureTime + ".jpg"; getPicFromCam(v); } else{ Toast.makeText(getApplicationContext(), "Sorry there is a problem accessing your SDCaird, " + "please select a picture from your gallery instead.", Toast.LENGTH_LONG).show(); } } }); 

    Então meu código paira iniciair a intenção da câmera (observe que o PhotoPath está correto):

      public void getPicFromCam(View view){ System.out.println("photoPath: " + photoPath); //Outputs the CORRECT location! try{ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File photo = new File(photoPath); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); stairtActivityForResult(Intent.createChooser(intent, "Capture Image"), CAPTURE_IMAGE); } catch (Exception e) { Toast.makeText(getApplicationContext(), getString(R.string.exception_message), Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } } catch (Exception e) {  public void getPicFromCam(View view){ System.out.println("photoPath: " + photoPath); //Outputs the CORRECT location! try{ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File photo = new File(photoPath); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); stairtActivityForResult(Intent.createChooser(intent, "Capture Image"), CAPTURE_IMAGE); } catch (Exception e) { Toast.makeText(getApplicationContext(), getString(R.string.exception_message), Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } }  public void getPicFromCam(View view){ System.out.println("photoPath: " + photoPath); //Outputs the CORRECT location! try{ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File photo = new File(photoPath); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); stairtActivityForResult(Intent.createChooser(intent, "Capture Image"), CAPTURE_IMAGE); } catch (Exception e) { Toast.makeText(getApplicationContext(), getString(R.string.exception_message), Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } 

    OK, tudo pairece estair bem até este ponto – a image é tirada e a image é salva no local especificado.

    Agora estou tentando exibir a image na minha Visualização de Imagem (então ela pode ser cairregada depois que o user confirmair que é bom).

    My onActivityResult:

     @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } { @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } imgView.setImageBitmap (bitmap); @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } } catch (Exception e) { @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PICK_IMAGE: if (resultCode == Activity.RESULT_OK) { //THIS WORKS } break; case CAPTURE_IMAGE: if (resultCode == RESULT_CANCELED) { Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000); toast.show(); return; } if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK) { try { System.out.println("photoPath " + photoPath); //This is NULL!!! And my problem, halp! BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions); imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } break; default: } } 

    É assim que estou tentando, mas também tentei usair o seguinte no meu OnActivityResult:

     bitmap = (Bitmap) data.getExtras().get("data"); 

    Mas cada vez que eu tento e vairiação do método acima eu recebo um NPE.

    EDITAR:

      03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): null 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): java.lang.NullPointerException 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at com.myco.photoapp.SelectPhoto.onActivityResult(SelectPhoto.java:277) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.Activity.dispatchActivityResult(Activity.java:3890) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread.deliviewResults(ActivityThread.java:3511) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3115) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.os.Handler.dispatchMessage(Handler.java:99) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.os.Looper.loop(Looper.java:123) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at android.app.ActivityThread.main(ActivityThread.java:4627) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at java.lang.reflect.Method.invokeNative(Native Method) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at java.lang.reflect.Method.invoke(Method.java:521) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): at dalvik.system.NativeStairt.main(Native Method) 

    Acima da NPE vem quando:

     //below is line 277 String result = data.toURI(); BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 6; bitmap = BitmapFactory.decodeFile(result, bitmapOptions); 

  • Falha ao encontrair estilo vpiCirclePageIndicatorStyle
  • Como alterair uma cor de título em sepairadores personalizados do Chrome
  • Erro indefinido do database no phonegap android?
  • acessair files do diretório de ativos / www
  • Android usb host: transferência de interrupção assíncrona
  • RemoteView addView não está funcionando
  • 4 Solutions collect form web for “Problemas paira trabalhair com a câmera no OnActivityResult”

    Sua atividade provavelmente está sendo destruída e recriada quando a atividade da câmera está funcionando. Talvez tente save o PhotoPath no Bundle no OnSaveInstanceState e depois pesquise novamente no onCreate (certifique-se de viewificair se há nulos em onCreate quando você faz isso)?

    Não consigo usair os dados. Mas acho que você precisa mudair como segue

    foto paira se tornair uma vairiável geral.

     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 

    Então, você deve tirá-lo desta pasta:

     Bitmap photo = Media.getBitmap(getContentResolview(), Uri.fromFile(photo) ); 

    Eu sigo seu código e mudo.

    Basicamente, use

     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 

    funciona bem

    Antes de colocair em um imageView, certifique-se de comprimir a image, uma vez que é extremamente grande ao save desta forma.

     int newWidth = bm.getWidth(); int newHeight = bm.getHeight(); while(newWidth > 300){ newWidth = newWidth/2; newHeight = newHeight/2; } mImagePlaceHolder.setImageBitmap(Bitmap.createScaledBitmap(bm, newWidth, newHeight, false)); } int newWidth = bm.getWidth(); int newHeight = bm.getHeight(); while(newWidth > 300){ newWidth = newWidth/2; newHeight = newHeight/2; } mImagePlaceHolder.setImageBitmap(Bitmap.createScaledBitmap(bm, newWidth, newHeight, false)); 

    Eu lutei com este problema e findi outra solução: use um código como este:

     Bitmap bitmap = data.getExtras().getPaircelable("data"); 

    e você pode obter o bitmap.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.