Android gets the picture from the System gallery concrete implementation _android

Source: Internet
Author: User

Objective

In Android applications, there are often scenes that need to be used to store pictures on the device, and it's very inconvenient to get directly from the path. So it is generally recommended to call the gallery application of the system, select the picture, and then use it. This blog will explain how to get pictures from the System Gallery in Android.

Gallery Application

Android has a lot of apps built into it, and gallery is a gallery for manipulating pictures on the device, and it proactively scans for images stored on the device when it's powered on, and can use gallery to manipulate them. If you want to use gallery, look at its androidmanifest.xml manifest file first.

Copy Code code as follows:

<activity android:name= "Com.android.camera.ImageGallery"
Android:label= "@string/gallery_label"
Android:configchanges= "Orientation|keyboardhidden"
android:icon= "@drawable/ic_launcher_gallery" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.VIEW"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:mimetype= "Vnd.android.cursor.dir/image"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.VIEW"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:mimetype= "Vnd.android.cursor.dir/video"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.GET_CONTENT"/>
<category android:name= "Android.intent.category.OPENABLE"/>
<data android:mimetype= "Vnd.android.cursor.dir/image"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.GET_CONTENT"/>
<category android:name= "Android.intent.category.OPENABLE"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:mimetype= "image/*"/>
<data android:mimetype= "video/*"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.PICK"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:mimetype= "image/*"/>
<data android:mimetype= "video/*"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.PICK"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:mimetype= "Vnd.android.cursor.dir/image"/>
</intent-filter>
</activity>

The above is part of the code in the gallery androidmanifest.xml file, showing the Imagegallery, from the numerous intent-filter, you can see that the selected picture should use " Android.intent.action.PICK ", it has two minitype," image/* "is used to get pictures," video/* "is used to get video. The number of action strings in Android is actually encapsulated in the intent class, Android.intent.action.PICK is no exception, it is Intent.action_pick.

Now that you know the start of the gallery action, then look at the source of Imagegallery.java, look for the selected picture after the return value.

Copy Code code as follows:

private void Launchcropperorfinish (Iimage img) {
Bundle Myextras = Getintent (). Getextras ();

Long size = Menuhelper.getimagefilesize (IMG);
if (Size < 0) {
The return if the image file isn't available.
Return
}

if (Size > Mvideosizelimit) {
Dialoginterface.onclicklistener Buttonlistener =
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Dialog.dismiss ();
}
};
New Alertdialog.builder (This)
. SetIcon (Android. R.drawable.ic_dialog_info)
. Settitle (R.string.file_info_title)
. Setmessage (R.string.video_exceed_mms_limit)
. Setneutralbutton (R.STRING.DETAILS_OK, Buttonlistener)
. Show ();
Return
}

        String cropvalue = Myextras!= null? myextras.getstring ("Crop"): null;< br>        if (cropvalue!= null) {
             Bundle Newextras = new Bundle ();
            if (cropvalue.equals ("Circle")) {
                newextras.putstring ( "Circlecrop", "true");
           }

Intent cropintent = new Intent ();
Cropintent.setdata (Img.fullsizeimageuri ());
Cropintent.setclass (this, cropimage.class);
Cropintent.putextras (Newextras);

/* pass through any extras which were passed in * *
Cropintent.putextras (Myextras);
Startactivityforresult (Cropintent, crop_msg);
} else {
Intent result = new Intent (null, Img.fullsizeimageuri ());
if (Myextras!= null && myextras.getboolean ("Return-data")) {
The size of a transaction should be below 100K.
Bitmap Bitmap = Img.fullsizebitmap (
iimage.unconstrained, 100 * 1024);
if (bitmap!= null) {
Result.putextra ("Data", bitmap);
}
}
Setresult (RESULT_OK, result);
Finish ();
}
}

The above Imagegallery.java part of the source code, from the Setresult () method can be seen, it returned intent contains the selected image of the URI, it is a content://the beginning of the content provider, And if the extra that passes past intent contains a name of "Return-data" and the value is true, the picture thumbnail with name "Data" is also written to the extra.

Gallery Get Picture Demo

Now that you know how to start the gallery action, and how it returns the selected data, then a simple demo will show you how to get the picture from the System gallery and show the captured picture to a imageview in the interface.

Copy Code code as follows:

Package Cn.bgxt.sysgallerydemo;

Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import android.app.Activity;
Import android.content.Intent;

public class Mainactivity extends activity {
Private Button btn_getimage;
Private ImageView Iv_image;
Private final static String TAG = "main";

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Btn_getimage = (Button) Findviewbyid (r.id.btn_getimage);
Iv_image = (ImageView) Findviewbyid (r.id.iv_image);

Btn_getimage.setonclicklistener (GetImage);
}

Private View.onclicklistener getImage = new Onclicklistener () {

        @Override
        public void OnClick (View v) {
           //Setting action and Minitype
            Intent Intent = new Intent ();
            intent.setaction (Intent.action_pick);
            intent.settype ("image/*");
           /To open an activity in a pattern that requires a return value
             Startactivityforresult (Intent, 0);
       }
   };

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
If successful, ResultCode is-1
LOG.I (TAG, "ResultCode:" + ResultCode);
if (Requestcode = = 0 && ResultCode = = 1) {
Gets the URI of the original image, which is a content provider
Uri uri = Data.getdata ();
Iv_image.setimageuri (URI);
}
Super.onactivityresult (Requestcode, ResultCode, data);
}
}

  Effect Display:

Summarize

This blog here basically explains how to cut down on Android with the system gallery get pictures, in fact, the function is very simple, mainly to pay attention to action and minitype do not write wrong, and the return value is a URI. Although now more and more need to use the image of commercial applications, are developing their own to obtain the function of the device picture, but the use of the System Gallery to obtain a fast implementation of the function of the solution. For convenience, the system's Gallery source code, will also be packaged into the source code, there is a need to download to see.

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.