The example of this article describes the Android development and implementation of the function of selecting photos from albums. To share with you for your reference, as follows:
Actual rendering:
Code implementation:
1. Permission configuration
2. Click event binding
3. Album access
4. Set the picture according to the path
5. Other methods
Jurisdiction
First, add the following permissions to the mainfest.xml file:
<! -- get photo permission -- >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Click events
Click to jump to album
imageView01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE
},1);
}
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_REQUEST_CODE);
}
});
The URI of pictures returned by different mobile phones is different, so it’s converted here
Can not add (if not, other methods are useless)
@TargetApi(19)
private void handleImageOmKitKat(Intent data){
String imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(this,uri)){
//If the document type is u day, it is processed by document ID
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())){
String id = docid.split (":") [1]; // the number format ID is parsed
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
imagePath = getImagePath(contentUri,null);
}
}else if ("content".equalsIgnoreCase(uri.getScheme())){
//If it's a normal type, handle it in a normal way
imagePath = getImagePath(uri,null);
}else if ("file".equalsIgnoreCase(uri.getScheme())){
//If the file type bit URI gets the path of the picture
imagePath = uri.getPath();
}
displayImage(imagePath);
}
About method: onactivityresult()
Set the avatar by putting back the path here, but there may be a certain delay in generating the image path, so a thread is opened here to wait:
/*Define a handler, define the delayed execution behavior*/
public void chnage(){
new Thread(){
@Override
public void run() {
while ( bitmap == null ){
bitmap = BitmapFactory.decodeFile(path);
Log.v("qwe","123");
}
Message message = handler.obtainMessage();
message.obj = 0;
handler.sendMessage(message);
}
}.start();
}
Other methods:
private void handleImageBeforeKitKat(Intent data){
Uri uri = data.getData();
String imagePath = getImagePath(uri,null);
displayImage(imagePath);
}
private String getImagePath(Uri uri, String selection){
String path = null;
//Get the real picture path through URI and selection
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null){
if (cursor.moveToFirst()){
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
private void displayImage(String imagePath){
if (imagePath != null){
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imageView01.setImageBitmap(bitmap);
}else {
Toast.makeText(MainActivity.this,"fail to get image",Toast.LENGTH_SHORT).show();
}
}
Related variables:
//Get pictures from album
Bitmap bitmap;
//Judge the returned activity
private static final int IMAGE_REQUEST_CODE = 0;
//Picture path
private String path ;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if((Integer)msg.obj==0){
imageView01.setImageBitmap(bitmap);
}
super.handleMessage(msg);
}
};
Demo source code click hereDownload this website。
For more information about Android related content, readers can view our special topics: summary of Android graphics and image processing skills, introduction and advanced course of Android development, summary of Android debugging skills and solutions to common problems, summary of usage of Android basic components, summary of Android view skills, summary of Android layout skills and an Droid Control Usage Summary
I hope this article is helpful for Android programming.