1

Código

public class primeiraTela extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primeira_tela);

        Button botaocan = findViewById(R.id.botaoCanid);


        botaocan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

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


        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK) {
                Intent fotoinfo = new Intent(this, fotocomInfo.class);
                startActivity(fotoinfo);
            } else {
            }


        }
    }
}

Erro:

E/AndroidRuntime: FATAL EXCEPTION: main Process: believe.com.br.believeundb, PID: 4189 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {believe.com.br.believeundb/believe.com.br.believeundb.primeiraTela}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4094) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4137) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at believe.com.br.believeundb.primeiraTela.onActivityResult(primeiraTela.java:41) at android.app.Activity.dispatchActivityResult(Activity.java:6931) at android.app.ActivityThread.deliverResults(ActivityThread.java:4090)

1 Resposta 1

0

Tente dessa forma dentro do seu onActivityResult:

        if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE_RC) {
        Uri photoUri;
        if (data != null && data.getData() != null) {
            photoUri = data.getData();
        }

        String path = photoUri.getPath();

        try {
            InputStream imageStream = getContentResolver().openInputStream(photoUri);
            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
            //seu código aqui
            imageStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .