Get uri from album or camera to bitmap

Source: Internet
Author: User
  Package com. amani. main;

Import java. io. ByteArrayOutputStream;
Import java. io. InputStream;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. content. ContentResolver;
Import android. content. DialogInterface;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. ImageView;

Public class chooser extends Activity {

Private ImageView imageView;
Private OnClickListener imgViewListener;
Bitmap myBitmap;
Private byte [] mContent;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ImageView = (ImageView) findViewById (R. id. ivPic );

ImgViewListener = new OnClickListener (){
Public void onClick (View v ){
Final CharSequence [] items = {"album", "photo "};
AlertDialog dlg = new AlertDialog. Builder (chooser. this). setTitle ("select image"). setItems (items,
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int item ){
// Here, item is based on the selected method. Two methods are defined in the items array. The subscript of the image is 1, so the photo method is called.
If (item = 1 ){
Intent getImageByCamera = new Intent ("android. media. action. IMAGE_CAPTURE ");
StartActivityForResult (getImageByCamera, 1 );
} Else {
Intent getImage = new Intent (Intent. ACTION_GET_CONTENT );
GetImage. addCategory (Intent. CATEGORY_OPENABLE );
GetImage. setType ("image/jpeg ");
StartActivityForResult (getImage, 0 );
}
}
}). Create ();
Dlg. show ();
}
};
// Bind the click listener to the imageView Control
ImageView. setOnClickListener (imgViewListener );

}

@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){

ContentResolver resolver = getContentResolver ();
/**
* Because the startActivityForResult method is used in both methods, the onActivityResult method is executed after the method is executed,
* In order to determine whether to choose the method to obtain the image, the requestCode here corresponds to the second parameter in startActivityForResult.
*/
If (requestCode = 0 ){
Try {
// Obtain the image uri
Uri originalUri = data. getData ();
// Parse the image content into a byte array
MContent = readStream (resolver. openInputStream (Uri. parse (originalUri. toString ())));
// Convert the byte array to a Bitmap object that can be called by ImageView.
MyBitmap = getPicFromBytes (mContent, null );
//// Bind the obtained image to the control for display
ImageView. setImageBitmap (myBitmap );
} Catch (Exception e ){
System. out. println (e. getMessage ());
}

} Else if (requestCode = 1 ){
Try {
Super. onActivityResult (requestCode, resultCode, data );
Bundle extras = data. getExtras ();
MyBitmap = (Bitmap) extras. get ("data ");
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
MyBitmap. compress (Bitmap. CompressFormat. JPEG, 100, baos );
MContent = baos. toByteArray ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Bind the obtained image to the control for display
ImageView. setImageBitmap (myBitmap );
}

}

Public static Bitmap getPicFromBytes (byte [] bytes, BitmapFactory. Options opts ){
If (bytes! = Null)
If (opts! = Null)
Return BitmapFactory. decodeByteArray (bytes, 0, bytes. length, opts );
Else
Return BitmapFactory. decodeByteArray (bytes, 0, bytes. length );
Return null;
}

Public static byte [] readStream (InputStream inStream) throws Exception {
Byte [] buffer = new byte [1024];
Int len =-1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream ();
While (len = inStream. read (buffer ))! =-1 ){
OutStream. write (buffer, 0, len );
}
Byte [] data = outStream. toByteArray ();
OutStream. close ();
InStream. close ();
Return data;

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.