I’m Having Trouble Opening a New Activity after taking a photo with android studio

Asked

Viewed 46 times

1

Code

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 {
            }


        }
    }
}

Error:

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 answer

0


Try that way inside your 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();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.