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