Android to write an activity to open a picture yourself

Source: Internet
Author: User

According to the memory of EoE intent related video, imitate, write an activity to open the picture

1, in the main activity button time, by setting the action, category, data to open a picture. Now that the code is ready to run, the image will be opened using the system default tool.

Newnew File ("/storage/emulated/legacy/download/xhtml.jpg""image/*"); StartActivity (intentimage);

  

2, you define an activity, used to display pictures. You can add a corresponding layout file, or you can use ImageView to render the picture without adding the layout file. The corresponding activity tag needs to be added to the manifest. When you click the button again, the Select application will appear.

    protectedvoid  onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);         New ImageView (this);        Setcontentview (ImageView);                 = getintent ();        Imageview.setimageuri (Imageintent.getdata ());    }
        <ActivityAndroid:name= "Showimageactivity">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.VIEW"/>                <categoryAndroid:name= "Android.intent.category.DEFAULT"/>                <DataAndroid:mimetype= "image/*"Android:scheme= "File"/>            </Intent-filter>        </Activity>

3, after the above processing, has been able to choose their own picture browser. But when you choose to use your own image browser to open the picture, is a blank, nothing, and using the system default tools, you can open the picture normally. Open Logcat found the following warning message " java.io.FileNotFoundException:/storage/emulated/legacy/download/xhtml.jpg:open failed: Eacces (Permission denied)", online search can be found, because there is no access to the directory permissions, you can refer to this blog Android read the image of the SD card. Add permissions in the manifest file, this time the program will work.

<android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

In the process of writing this program, find the following areas to note:

    • In the 1th step of the code, although the corresponding directory in file is actually pointing to the contents of the/mnt/shell/emulated/0 directory, but because the shell directory permissions are drwx------, there is no access to this directory, so you can only use the following directory to access the corresponding picture
    • In the 1th step of the code, Intentimage.setdata (file) to set the data, you can not open the picture, error "Android.content.ActivityNotFoundException: No activity found to handle Intent ", unable to find activity, through the error can be known, do not set the type, the system cannot find its matching Activity. In order to test, you can set the <data> tag in the XML file of step 2nd, do not set mimetype, only scheme. The code in the 1th step does not set the type to Intentimage.setdata (uri.fromfile (file)). Found at this time will not error, will automatically call yourself to write this image browser.

Android to write an activity to open a picture yourself

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.