0

I have the following code, but i don't know whether it is correct or not, in the app it crashes and shows NullPointer Exception in Logcat at : Bundle extras = intent.getExtras();

Here's the code which opens the camera intent:

public class AddImage extends Fragment {

Button button;
View view;
private static final int REQUEST_CODE_FROM_CAMERA = 100;
private Uri fileUri;
String image_path = "";
Button button1;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view =  inflater.inflate(R.layout.fragment_add_image, container, false);
    button = (Button)view.findViewById(R.id.addImage);
    button1 = (Button)view.findViewById(R.id.goImage);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent iSecond=new Intent(getActivity(),ShowImage.class);
            iSecond.putExtra("image_path",image_path);
            startActivity(iSecond);
        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(),"Launching Camera",Toast.LENGTH_SHORT).show();

            Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
            imagesFolder.mkdirs(); // <----
            File image = new File(imagesFolder, "image_001.jpg");
            Uri uriSavedImage = Uri.fromFile(image);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            startActivityForResult(imageIntent, 0);


        }
    });
    return view;
}






@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_FROM_CAMERA
            && resultCode == Activity.RESULT_OK) {
        try {


            image_path = fileUri.getPath();



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

    }
}







public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}

}

The receiving Activity:

public class ShowImage extends AppCompatActivity {

Intent intent;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    iv = (ImageView) findViewById(R.id.showImage);
    Bundle extras = intent.getExtras();
    if(extras != null) {

        String image_path = extras.getString("image_path");
    }
    File imgFile = new File("/storage/emulated/0/1426484497.png");

    if (imgFile.exists()) {

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                .getAbsolutePath());

        iv.setImageBitmap(myBitmap);

    }
}

}

NullPointer Exception at Bundle extras = intent.getExtras(); Can anyone help me out!

P.S also how to show the captured images in GridView in the receiving activity!

bappi bazzi
  • 225
  • 1
  • 3
  • 12
  • Why ask same question over and over http://stackoverflow.com/questions/32508033/receive-photo-from-previous-activity-to-the-current-one/32516867?noredirect=1#comment52892556_32516867 – Cüneyt Sep 11 '15 at 09:03

3 Answers3

0

Here

Bundle extras = intent.getExtras();

NullPointer Exception is occurring because intent is null.

initialize intent as:

intent= getIntent();

before calling getExtras .

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

make sure you declared External storage read write permission in manifest!

and change,

Bundle extras = intent.getExtras();

to,

Bundle extras = getIntent().getExtras();

Mayur R. Amipara
  • 1,223
  • 1
  • 11
  • 32
  • I got that but the image captured is not appearing in the second activity, can you look at the camera intent code and please tell where i've gone wrong – bappi bazzi Sep 11 '15 at 09:34
0

change startActivityForResult(imageIntent, 0); to startActivityForResult(imageIntent,REQUEST_CODE_FROM_CAMERA);

Albin Mathew
  • 411
  • 1
  • 6
  • 13