0

i want to check orientation of an image so i found some code but it doesn't work because cursor is always null. My code is

File f = createImageFile(bitmap);
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = context.getContentResolver().query(Uri.parse(f.toString()), orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
   orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);

private static File createImageFile(Bitmap bitmap) {
    //create a file to write bitmap data
    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    File f = new File(fullPath, "image"+ System.currentTimeMillis()+".jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    if (bitmap != null) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for PNG*/, bos);
    }
    byte[] bitmapdata = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f;
}

But cur always returns null.I searched for converting file to uri so i found two methods Uri.parse(f.toString() and Uri.fromFile(f) but none has worked.

Hardy Android
  • 855
  • 9
  • 20
Syed Muhammad Oan
  • 687
  • 2
  • 15
  • 39
  • Possible duplicate of [Android : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview?](http://stackoverflow.com/questions/12726860/android-how-to-detect-the-image-orientation-portrait-or-landscape-picked-fro) – xAqweRx May 19 '16 at 09:07
  • It won't work. Cause your new image is not in MediaStore. So you should check it by ExifData. – xAqweRx May 19 '16 at 09:08
  • i tried to use this way because ExifInterface was always giving 0 . I asked a question http://stackoverflow.com/questions/37306866/random-image-gets-rotated-when-using-picasso?noredirect=1#comment62158277_37306866 – Syed Muhammad Oan May 19 '16 at 10:00
  • Well, I would say, that you should take this original photo and check Meta info ( including orientation ) and check meta info of your downloaded image. – xAqweRx May 19 '16 at 10:28
  • can u explain?? how do i check meta info – Syed Muhammad Oan May 19 '16 at 10:35
  • You should download photo to your computer, and try look on http://helpdeskgeek.com/how-to/view-photo-exif-metadata-on-iphone-mac-and-windows/ – xAqweRx May 19 '16 at 10:38
  • another question . Why is my new image not in the media store because i saved the image and i am getting it the way i get other images?? I didn't understand that – Syed Muhammad Oan May 19 '16 at 15:35
  • Because MediaStore not updating in a second after saving image. There are plenty topics, kind of http://stackoverflow.com/q/3300137/2685996 – xAqweRx May 21 '16 at 06:22
  • And still => check meta info of your orignal photo and downloaded photo. – xAqweRx May 21 '16 at 06:23

0 Answers0