0

I am capturing an image and store it in storage in mobile but when I get this image it cannot show any thing in Image View. I have tried a lot of code to get images from file but none of them are working in my emulator or real Samsung device.

enter code here

 imageHolder = (ImageView)findViewById(R.id.captured_photo);
    Button capturedImageButton = (Button)findViewById(R.id.photo_button);
    capturedImageButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(photoCaptureIntent, requestCode);
        }
    });

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK){
        Bitmap bitmap = (Bitmap)data.getExtras().get("data");

        String partFilename = currentDateFormat();
        storeCameraPhotoInSDCard(bitmap, partFilename);


        // display the image from SD Card to ImageView Control
        String storeFilename = "photo_" + partFilename + ".jpg";
        Bitmap mBitmap = getImageFileFromSDCard(storeFilename);
        imageHolder.setImageBitmap(mBitmap);

    }
}

  private String currentDateFormat(){
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
    String  currentTimeStamp = dateFormat.format(new Date());
    return currentTimeStamp;
}

private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){
    File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg");
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private Bitmap getImageFileFromSDCard(String filename){
  /*  Bitmap bitmap = null;
    File imageFile = new File(Environment.getExternalStorageDirectory() + filename);
    try {
        FileInputStream fis = new FileInputStream(imageFile);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return bitmap; */

    File imageFile = new File(Environment.getExternalStorageDirectory() + filename);
   // File imgFile = new  File(filename);
            //("/sdcard/Images/test_image.jpg");
    Bitmap myBitmap;

    if(imageFile.exists()){

         myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

       // ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

      //  myImage.setImageBitmap(myBitmap);

        return myBitmap;

    }
    return null;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Yasir Ali
  • 155
  • 1
  • 11

1 Answers1

0

First of All make sure you have declared the "Access External Storage" and "Access Hardware Camera" permissions in "AndroidManifest" and if you are using Android version 23 or 23+ then you have to take permissions on run-time. If this is not the problem then use this code given below, it's working fine for me.

For Camera:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePicture, ACTION_REQUEST_CAMERA);

For Gallery:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");

            Intent chooser = Intent.createChooser(intent, "Choose a Picture");
            startActivityForResult(chooser, ACTION_REQUEST_GALLERY);

OnActivityResultMethod:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK)    {

        switch (requestCode) {
            case ACTION_REQUEST_GALLERY:
                Uri galleryImageUri = data.getData();
                try{
                    Log.e("Image Path Gallery" , getPath(getActivity() , galleryImageUri));
                    selectedImagePath = getPath(getActivity() , galleryImageUri);
                } catch (Exception ex){
                    ex.printStackTrace();
                    Log.e("Image Path Gallery" , galleryImageUri.getPath());
                    selectedImagePath = galleryImageUri.getPath();
                }

                break;

            case ACTION_REQUEST_CAMERA:
                // Uri cameraImageUri = initialURI;
                Uri cameraImageUri = data.getData();
                Log.e("Image Path Camera" , getPath(cameraImageUri));
                selectedImagePath = getPath(cameraImageUri);
                break;
        }

    }
}

Method to get path of Image returned from Camera:

/**
 * helper to retrieve the path of an image URI
 */
public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    // this is our fallback here
    return uri.getPath();
}
Zohaib Hassan
  • 984
  • 2
  • 7
  • 11