1

I am new on Android. I am testing an Android application (built after searching on internet) that creates an Intent in the main activity that is started onClick of a button, starts the Camera of the device and returns the photo taken to an ImageView in the main layout. The problem I have is that while testing on the emulator the process finishes successfully (the picture shows up on the ImageView after saving the photo), the same happens while testing on the Samsung Tablet 2 (GT-P7510) with Android version 4.0.4, but when I try to run it on the Samsung S4 smartphone (GT-I9500) with version 5.0.1 it saves the picture but the photo is not shown in ImageView on main layout.

Does someone had the same issue?

I read it could be to some problem on the OnCreate but couldn't solve it.

Here's part of the code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_camera);

    photoImage = (ImageView) findViewById(R.id.photo_image);

    callCameraButton = (Button) 
    findViewById(R.id.button_callcamera);

    photoImage.setVisibility(View.VISIBLE);

    callCameraButton.setOnClickListener( new View.OnClickListener() {
        public void onClick(View view) {
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = Uri.fromFile(getOutputPhotoFile());
            i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            archivo = fileUri.getEncodedPath();
            startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
        if (resultCode == RESULT_OK) {
          Uri photoUri = null;
          if (data == null) {
            // A known bug here! The image should have saved in fileUri
            Toast.makeText(this, "Image saved successfully", 
                           Toast.LENGTH_LONG).show();
            photoUri = fileUri;
          } else {
            photoUri = data.getData();
            Toast.makeText(this, "Image saved successfully in: " + data.getData(), 
                           Toast.LENGTH_LONG).show();
          }
          showPhoto(archivo);

        } else if (resultCode == RESULT_CANCELED) {
          Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(this, "Callout for image capture failed!", 
                         Toast.LENGTH_LONG).show();
        }
    }
}

private void showPhoto(String photoUri) {
      File imageFile = new File (photoUri);
      if (imageFile.exists()){
         Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
         BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
         photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
         photoImage.setImageDrawable(drawable);
      }       
}
Cloio
  • 11
  • 1
  • You don't need this showPhoto(String uri) method. You can use ImageView.setImageURI method that takes URI that you get from successful intent. Here is the link http://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri) – Nikola Milutinovic Sep 18 '15 at 00:13
  • Thank's Nikola. I tried already that but didn't work either. – Cloio Sep 18 '15 at 00:41
  • What I found out is that because of the large number of bytes the photo is taken by the S4 Camera, then that's the cause of not showing on the ImageView. I found a solution on the next link http://stackoverflow.com/questions/29932060/imageview-will-not-load-via-setimageuri-when-ran-on-samsung-galaxy-s4-only (thanks AJ9 !!) which worked for me. The only problem is now that the image appears 90 degrees rotated, and if picked in Portrait is always shown in Landscape. – Cloio Sep 18 '15 at 00:47
  • I posted code to fix the rotation. Please update your question so there is no confusion :) – Nikola Milutinovic Sep 18 '15 at 09:10
  • check if bitmap is null? Also there is no need to convert bitmap to drawable, you can directly set bitmap on ImageView! – Muhammad Babar Sep 18 '15 at 10:06
  • Thanks Muhammad for the tip. – Cloio Sep 18 '15 at 20:01

2 Answers2

0

Here is the solution for image rotation. That rotation will be present and will be different from device to device... So code:

private void fixImage(String filepath) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap b = BitmapFactory.decodeFile(filepath, options);

    ExifInterface ei = new ExifInterface(filepath);
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    float angle = 0;
    switch(orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            angle = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            angle = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            angle = 270;
            break;
        default:
            angle = 0;
    }
    saveImageToFile(rotateImage(b, angle), filepath);

}

public static Bitmap rotateImage(Bitmap source, float angle)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Nikola Milutinovic
  • 731
  • 1
  • 8
  • 23
  • Thanks Nikola. Unfortunately this solution doesn't works (I already tried it before). Anyway, I added your code to my code and now is worst, nothing appears on the S4 Screen Layout after saving the picture. – Cloio Sep 18 '15 at 19:51
  • private void showPhoto(String photoUri) throws IOException { File imageFile = new File (photoUri); if (imageFile.exists()){ photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER); String filepath = imageFile.getAbsolutePath(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap b = BitmapFactory.decodeFile(filepath, options); ExifInterface ei = new ExifInterface(filepath); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); – Cloio Sep 18 '15 at 19:58
  • float angle = 0; switch(orientation) { case ExifInterface.ORIENTATION_ROTATE_90: angle = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: angle = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: angle = 270; break; default: angle = 0; } photoImage.setImageBitmap(rotateImage(b, angle)); – Cloio Sep 18 '15 at 19:58
0

Finally here is how I solved the problem with your help and some other search.

private void showPhoto(String photoUri) throws IOException {
      File imageFile = new File (photoUri);
      if (imageFile.exists()){
            photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
            String filepath = imageFile.getAbsolutePath();
            Bitmap b = createScaledBitmap(filepath, photoImage.getWidth(), photoImage.getHeight());
            ExifInterface ei = new ExifInterface(filepath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            float angle = 0;
            switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    angle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    angle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    angle = 270;
                    break;
                default:
                    angle = 0;
            }
            photoImage.setImageBitmap(RotateBitmap(b, angle));
      }
}

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Cloio
  • 11
  • 1