Android Image Rendering (6)-obtaining local images or taking pictures

Source: Internet
Author: User

Obtain image resources from the SD card or take a new image.

Post Code first

Get image:


Note: You can specify the address to save the image when taking the photo.


[Java]
CharSequence [] items = {"album", "camera "};
New AlertDialog. Builder (this)
. SetTitle ("select image source ")
. SetItems (items, new OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
If (which = SELECT_PICTURE ){
Intent intent = new Intent (Intent. ACTION_GET_CONTENT );
Intent. addCategory (Intent. CATEGORY_OPENABLE );
Intent. setType ("image /*");
StartActivityForResult (Intent. createChooser (intent, "select image"), SELECT_PICTURE );
} Else {
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, SELECT_CAMER );
}
}
})
. Create (). show ();
CharSequence [] items = {"album", "camera "};
New AlertDialog. Builder (this)
. SetTitle ("select image source ")
. SetItems (items, new OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
If (which = SELECT_PICTURE ){
Intent intent = new Intent (Intent. ACTION_GET_CONTENT );
Intent. addCategory (Intent. CATEGORY_OPENABLE );
Intent. setType ("image /*");
StartActivityForResult (Intent. createChooser (intent, "select image"), SELECT_PICTURE );
} Else {
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, SELECT_CAMER );
}
}
})
. Create (). show ();

 

Method 1: process the returned image directly:


Note:

1. There are instructions on the Internet that the system compresses the returned images directly, but there is no difference in the test process;


2. If you continuously retrieve images, you must release the current Bmp memory; otherwise, an error will be reported! Bmp. recycle ().


[Java]
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (resultCode = RESULT_ OK ){
// Select an image
Uri uri = data. getData ();
ContentResolver cr = this. getContentResolver ();
Try {
If (bmp! = Null) // if the image is not released, the memory will be insufficient if the image is continuously retrieved.
Bmp. recycle ();
Bmp = BitmapFactory. decodeStream (cr. openInputStream (uri ));
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("the bmp toString:" + bmp );
ImageSV. setBmp (bmp );
} Else {
Toast. makeText (SetImageActivity. this, "Please reselect the image", Toast. LENGTH_SHORT). show ();
}

}
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (resultCode = RESULT_ OK ){
// Select an image
Uri uri = data. getData ();
ContentResolver cr = this. getContentResolver ();
Try {
If (bmp! = Null) // if the image is not released, the memory will be insufficient if the image is continuously retrieved.
Bmp. recycle ();
Bmp = BitmapFactory. decodeStream (cr. openInputStream (uri ));
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("the bmp toString:" + bmp );
ImageSV. setBmp (bmp );
} Else {
Toast. makeText (SetImageActivity. this, "Please reselect the image", Toast. LENGTH_SHORT). show ();
}

}

 

Method 2: Obtain the image address and then process the image:

 

[Java]
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (resultCode = RESULT_ OK ){
Uri uri = data. getData ();
String [] proj = {MediaStore. Images. Media. DATA };
Cursor cursor = managedQuery (uri,
Proj, // Which columns to return
Null, // WHERE clause; which rows to return (all rows)
Null, // WHERE clause selection arguments (none)
Null); // Order-by clause (ascending by name)

Int column_index = cursor. getColumnIndexOrThrow (MediaStore. Images. Media. DATA );
Cursor. moveToFirst ();

String path = cursor. getString (column_index );
Bmp = BitmapFactory. decodeFile (path );
System. out. println ("the path is:" + path );
} Else {
Toast. makeText (SetImageActivity. this, "Please reselect the image", Toast. LENGTH_SHORT). show ();
}

}
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (resultCode = RESULT_ OK ){
Uri uri = data. getData ();
String [] proj = {MediaStore. Images. Media. DATA };
Cursor cursor = managedQuery (uri,
Proj, // Which columns to return
Null, // WHERE clause; which rows to return (all rows)
Null, // WHERE clause selection arguments (none)
Null); // Order-by clause (ascending by name)

Int column_index = cursor. getColumnIndexOrThrow (MediaStore. Images. Media. DATA );
Cursor. moveToFirst ();

String path = cursor. getString (column_index );
Bmp = BitmapFactory. decodeFile (path );
System. out. println ("the path is:" + path );
} Else {
Toast. makeText (SetImageActivity. this, "Please reselect the image", Toast. LENGTH_SHORT). show ();
}

}

From Akai's column
 

Related Article

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.