Good day. I am new and need help.

I wrote a program in which the user selects an image from the gallery or shoots around the camera and this image is displayed in MainActivity , then you need to transfer this picture to another Activity .

I wrote it this way, but at the time of sending the application crashes. Help me fix please.

Here I have already received the image and set it in ImageView and I want to pass, but the exception NullPointerException : Main is displayed:

 Uri selectedImage; ImageView preview; ImageButton gallery; ImageButton camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery = (ImageButton) findViewById(R.id.gallery_button); camera = (ImageButton) findViewById(R.id.camera_button); preview = (ImageView) findViewById(R.id.selected_image); camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent takepic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takepic, 0); } }); gallery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent choosegal = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(choosegal, 1); } }); preview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent choose = new Intent(getApplicationContext(), Second_Activity.class); choose.putExtra("img", selectedImage.toString()); startActivity(choose); } }); } @Override protected void onActivityResult(int req, int res, Intent imagereturn) { super.onActivityResult(req, res, imagereturn); switch (req) { case 0: if (res == RESULT_OK) { selectedImage = imagereturn.getData(); preview.setImageURI(selectedImage); } break; case 1: if (res == RESULT_OK) { selectedImage = imagereturn.getData(); preview.setImageURI(selectedImage); } break; } } 

Second:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); ImageView image=(ImageView)findViewById(R.id.ramka); // } 
  • Show the error itself. - Vladimir Glinskikh
  • choose.putExtra ("img", selectedImage.toString ()); during this method, the application stops, went through a debag, and then saw that an exception was thrown. if you want I can show all the code - androidx
  • and that's why I'm here. can you help ? - androidx
  • do you need to transfer a Bitmap or which object? - pavlofff
  • selectedImage object Uri - androidx

1 answer 1

In order to pass an Uri object through Intent you need to convert it into a string when sending.

shipping:

 intent.putExtra("img", selectedImage.toString()); 

getting in another activity:

 Uri uri = Uri.parse(extras.getString("img")); 

or parse URI itself.

shipping:

 intent.putExtra("img", selectedImage); 

receiving:

 Uri uri = intent.getParcelableExtra("img"); 
  • but here is NullPointerException and of course sorry for the question. but extras is what object in my code? - androidx
  • extras is a method that transmits / receives data. In your code, you transmit \ get generally unclear what. Send the path to the picture (path) and try to get the number (int) - pavlofff
  • No, I tried it in your way too. again the application stops - androidx
  • Then snoop your code in the question in the current state, attach the frame when it falls and specify the line on which it falls - pavlofff
  • I specified)) here choose.putExtra ("img", selectedImage.toString ()); - androidx