0

My Activity has 2 buttons, one for the camera and another one to send data to the server, the problem is that when i close the application i can't set a new image on my ImageView, and when i take a picture i can't use the "send" button. I need find a way that when i take a photo i still can use the rest of my activity, i just want to set the imageView to see the image that i got from the camera, and when i close the application, i want to be able to set a new photo (it keeps giving me a blank imageView).

Here i initialize the camera.

button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }

            }

        });

And here i set the imageView with the bitmap that i got from the camera.

protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setContentView(R.layout.comprovante_geral);
        final ImageView photo_view = (ImageView) findViewById(R.id.photo_view);

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            photo_view.setImageBitmap(imageBitmap);
        }

1 Answers1

0

replace this onActivityResult method with below code.

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
  if (requestCode == 1 && resultCode == RESULT_OK)
  {               
    Uri selectedImageUri = data.getData();
    String imagepath = getPath(selectedImageUri); 
    File imageFile = new File(imagepath);
  }
}
public String getPath(Uri uri) 
{
  String[] projection = { MediaStore.Images.Media.DATA };
  Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();                
  int columnIndex = cursor.getColumnIndex(projection[0]);
  String filePath = cursor.getString(columnIndex);
  cursor.close();        
  yourSelectedImage = BitmapFactory.decodeFile(filePath);
  return cursor.getString(column_index);
}

i hope this helps u.

Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80