Android Open Source - android-intent Image Utils






From Project

Back to project page android-intent.

License

The source code is released under:

MIT License

If you think the Android project android-intent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.intentsample;
/*from  w  w w . jav  a 2s.  com*/
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore.Images;

public class ImageUtils {
  public static Bitmap decodeUri(Context context, Uri uri){
    if(uri == null)return null;
    try{
      // ?????????????
      InputStream input = context.getContentResolver().openInputStream(uri);
      Bitmap bitmap = BitmapFactory.decodeStream(input);
      input.close();
      
      // ??????
      int r = 0;
      if(uri.getScheme().equals("content")){
        String[] projection = {Images.ImageColumns.ORIENTATION};
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        if(c.moveToFirst()){
          r = c.getInt(0);
        }
      }
      else{
        try{
          ExifInterface exif = new ExifInterface(uri.getPath());
          int orientation = exif.getAttributeInt(
              ExifInterface.TAG_ORIENTATION,
              ExifInterface.ORIENTATION_NORMAL);
          if(orientation == ExifInterface.ORIENTATION_ROTATE_90){
            r = 90;
          }
          else if(orientation == ExifInterface.ORIENTATION_ROTATE_180){
            r = 180;
          }
          else if(orientation == ExifInterface.ORIENTATION_ROTATE_270){
            r = 270;
          }
          else{
            r = 0;
          }
        }
        catch(IOException e){
          e.printStackTrace();
        }
      }
      
      // ??
      Matrix matrix = new Matrix();
      matrix.postRotate(r, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
      return bitmap;
    }
    catch(Exception e){
      e.printStackTrace();
    }
    return null;
  }
}




Java Source Code List

com.example.intentsample.ImageUtils.java
com.example.intentsample.MainActivity.java
com.example.intentsample.SubActivity.java