0

I am trying to use the following code to take a picture with the camera and display it in a ImageView

public void takepic(View view) {

    String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());
    String imageFileName = timeStamp + ".jpg";
    TextView detail = (TextView)findViewById(R.id.textView1);
    detail.setText(imageFileName); 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String name = imageFileName;


            File file = new File(path, name);
            outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, TAKE_PICTURE); 


}

static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch(requestCode) {

    case 3:
        if (resultCode == RESULT_OK){

                File imgFile = new  File(outputFileUri.toString());

                TextView detail = (TextView)findViewById(R.id.textView1);
                detail.setText(imgFile.toString()); 

            if(imgFile.exists()){

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

                ImageView myImage = (ImageView) findViewById(R.id.mImageView);
                myImage.setImageBitmap(myBitmap);

            }
            else{
                Toast.makeText(getBaseContext(), "Doesnt Exist", Toast.LENGTH_LONG).show();

            }
        }}}

Problem is its not displaying the picture

It takes the picture gives it a filename and saves it in the pictures directory.

when it gets to the onActivityResult outputFileUri.toString() gives the following

"file:/storage/emulated/0/Pictures/03-09-2014-06-53-04.jpg"

when i check the pictures directory the picture is there and is spelled correctly

but when it goes to the imgFile.exists if statement it says it doesn't exist and toasts the else toast

Any ideas where i'm going wrong?

any help appreciated

Mark

N J
  • 27,217
  • 13
  • 76
  • 96
user3422687
  • 229
  • 1
  • 8
  • 27
  • Have a Look http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity – Naveen Tamrakar Sep 03 '14 at 06:09
  • Your `data` will already contain the image. So just try `Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo);` – MysticMagicϡ Sep 03 '14 at 06:11

2 Answers2

0

In onActivityResult if your case is RESULT_OK, your parameter data would already be having the captured bitmap.

So you can try this:

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
imageView.setImageBitmap(photo);

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

use this code:

 if(imgFile!=null){


Bitmap myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgFile.toString());
 ImageView myImage = (ImageView) findViewById(R.id.mImageView);
                myImage.setImageBitmap(myBitmap);

}
Danubian Sailor
  • 1
  • 38
  • 145
  • 223
Dakshesh Khatri
  • 639
  • 7
  • 12
  • I tried this but get an error in the getbitmap line saying "The method getBitmap(ContentResolver, Uri) in the type MediaStore.Images.Media is not applicable for the arguments (ContentResolver, String)" – user3422687 Sep 03 '14 at 17:34
  • Thank you I work out what was missing i needed to add this.imageView = (ImageView)this.findViewById(R.id.mImageView); – user3422687 Sep 03 '14 at 22:52