Android uses implicit intent to open an image

Source: Internet
Author: User

Implementation features

?? Click "View Picture" to be able to jump out of the prompt, select the System library open or your own app to open, and for the download of good pictures also valid.

  

1. I put the Qiaoba. jpg in the res/drawable directory, open the picture via the button settings, and listen to the button in the following settings:

Findviewbyid (r.id.btn_openimage). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//If the custom app opens the picture, you must add Action_view intentIntent Intent =NewIntent (Intent.action_view); //make a picture-->bitmap-->uri conversionBitmap Bitmap =Bitmapfactory.decoderesource (Getresources (), R.drawable.qiaoba); //The system provides a ready-made API for users to save their favorite images to a system album.String uristring = MediaStore.Images.Media.insertImage (Getcontentresolver (), Bitmap,NULL,NULL);                System.out.println (uristring); Uri URI=Uri.parse (uristring); //setting the data and type can be used SetData () or Settype (), but the settings are not valid at the same time .//You can use only Setdataandtype (), the image represents a picture, and the asterisk represents all the formatting in the picture .//can be filtered according to their needs, such as only want to open the JPG type picture available "Image/jpg"Intent.setdataandtype (URI, "image/*");            StartActivity (Intent); }        });

2. Before sending a request to open a picture, add a Java file "Showimageaty.java" to display the picture, and add a corresponding "aty_show_image.xml" file

 Public classShowimageatyextendsappcompatactivity {ImageView ivqiaoba; @Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.aty_show_image); Ivqiaoba=(ImageView) Findviewbyid (R.id.iv_qiaoba); Intent GetImage=getintent (); //not for empty judgment        if(GetImage! =NULL) {            //gets the URI data passed by the intent .Uri data =Getimage.getdata (); if(Data! =NULL) {Ivqiaoba.setimageuri (data); }        }    }}

3. Set the Intent-filter tag in the Androidmanifest.xml file and register the network and file read and write permissions

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.jikexueyuan.notepad.simplepicturebrowser" > <!--access network rights--<uses-permission android:name= "Android . permission. INTERNET "/> <!--read and write SD card permissions--<uses-permission android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEM S "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permission Android Oid:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <Application Android:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G                T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity&        Gt <Activity Android:name=". Showimageaty "android:exported= "true" > <intent-filter> <action android:name= "Android.intent.action.VIEW"/> <category android:name= "Android.intent.category.DEFAULT"/> <!--This property is used to set the MIME type of the data, such as: I Mage/jpeg or audio/mpeg4-Generic. Its subtypes are available with the asterisk wildcard character (*) to indicate the ability to match any subtype.     --<data android:mimetype= "image/*"/> </intent-filter> </activity> </application></manifest>

In addition, if the android6.0,targetsdkversion more than 23 run will be manually registered read and Write permission, if the test version above 23, will report such a mistake

Java.lang.SecurityException:Permission denial:writing com.android.providers.media.MediaProvider URI content:// Media/external/images/media from pid=2317, uid=10064 requires Android.permission.WRITE_EXTERNAL_STORAGE, or Granturipermission ()

Manual registration Read and write permission see: http://blog.csdn.net/dzsw0117/article/details/51212612

Android uses implicit intent to open an image

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.