android image drawing of 6 get local pictures or take pictures and other picture resources

  • 2020-05-09 19:16:25
  • OfStack

Get an image resource from the SD card, or take a new image.
First post code
Get pictures:
Note: if you take a photo, you can specify the saving address of the picture, which is not explained here.
 
CharSequence[] items = {" Photo album ", " The 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, " Choose picture "), SELECT_PICTURE); 
}else{ 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, SELECT_CAMER); 
} 
} 
}) 
.create().show(); 

Process the image, method 1, directly process the returned image:
Note:
1. There are instructions on the Internet that the images returned by direct processing are compressed by the system, but there is no difference in the process of testing;
2. If the user keeps getting pictures again and again, the current Bmp memory must be freed, otherwise an error will be reported! bmp. recycle ().
 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
if(resultCode == RESULT_OK){ 
// Choose picture  
Uri uri = data.getData(); 
ContentResolver cr = this.getContentResolver(); 
try { 
if(bmp != null)// If you don't, you'll run out of memory  
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(); 
} 
} 

Process the image, method 2, get the image address and process it:
 
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(); 
} 
} 

Related articles: