0

In my Activity A, there is an ImageView and a Button. When the button is clicked, it goes to activeTakePhoto(). The imgUri gets displayed but nothing is displayed in my ImageView.

 private void activeTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver()
                    .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            values);
            takePictureIntent
                    .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

 @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                              Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE &&
                        resultCode == RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver()
                            .query(selectedImage, filePathColumn, null, null,
                                    null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                }
            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE &&
                        resultCode == RESULT_OK) {
                    File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
                    ImageView imgView=(ImageView)findViewById(R.id.imageView);
                    Uri imgUri=Uri.fromFile(picture);
                    imgView.setImageURI(imgUri);
                    Toast.makeText(getApplication(),imgUri+"",Toast.LENGTH_LONG).show();
                }
        }
    }
Tony
  • 2,515
  • 14
  • 38
  • 71

2 Answers2

1

You can try this code,it may help:

@Override
public void onClick(View v) {

    if (v == imgCamera) {

        Toast.makeText(getApplicationContext(), "open camera", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST);
    }
}//on click

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

    super.onActivityResult(requestCode, resultCode, data);
    Log.e("RESULT CODE", "--" + resultCode);
    if (resultCode == RESULT_OK) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        //to generate random file name
         String fileName = "tempimg.jpg";

        try {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            //captured image set in imageview
            imageView.setImageBitmap(photo);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
User_1191
  • 981
  • 2
  • 8
  • 24
  • `Error:(143, 23) error: exception FileNotFoundException is never thrown in body of corresponding try statement` and `Error:(145, 23) error: exception IOException is never thrown in body of corresponding try statement` – Tony Dec 09 '15 at 13:17
  • remove both `catch` statements and only and `Exception` like, `catch(Exception e){ }` – User_1191 Dec 09 '15 at 13:40
  • I get crashed :( why ? http://stackoverflow.com/questions/34195640/app-crashed-when-image-from-gallery-get-selected –  Dec 10 '15 at 07:11
0

in activeTakePhoto

Replace

String fileName = "temp.jpg"; 

with

fileName=Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.jpg"
Nitin Mesta
  • 1,504
  • 19
  • 32