Android Development-Choose a picture from the album without clipping

Source: Internet
Author: User

Reprint Please specify Source: http://blog.csdn.net/zhoubin1992/article/details/46864777

Questions:
In the first line of code in Guo Shen, the 8th chapter selects a picture from the album, which is cropped from the album and displayed to the screen. But after the implementation will be found from the album Selected pictures. No popup clipping interface, direct return.

Scheme:

When looking for a reason, it is found that the output_image.jpg under the SD card path is a 0-byte file. So
This picture is not generated. Then I think there is a problem with the intention of sending the selection photo to the system. I am curious to see the following system of Library application (gallery) source code of the manifest file intent filter This piece, sure enough to find:

<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>

Discover here with Android.intent.action.PICK action, you can choose a picture or video. And the book is Android.intent.action.GET_CONTENT.
Successfully jump to the cropping screen after the change.
Another problem, I use
Uri selectedimage = Data.getdata ();//Gets the URI of the photo returned by the system
The output shows that this is not an absolute path.

This is not right for us to set up on the imagevew or upload to the network.

Demo:
I'm giving you a demo from the photo album that doesn't crop. Tell us how to get the absolute path. Perfect execution.
First create a new project, edit the Activity_main.xml file, add a button to the layout to select a photo from the album, a ImageView. The code looks like the following:

<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"    Xmlns:tools="Http://schemas.android.com/tools"    Android:layout_width="Match_parent"    Android:layout_height="Match_parent"    Android:paddingbottom="@dimen/activity_vertical_margin"    Android:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"    android:orientation="Vertical"    Tools:context="Com.example.selectfromgallery.MainActivity">    <button  android:id  = "@+id/choose_from_album"  android:layout_ Width  = "match_parent"  android:layout_height  = "wrap_content"  android:text  =/>     <ImageViewandroid:id= "@+id/view"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_gravity=" Center _horizontal " />                                </linearlayout>

Then change the code in the mainactivity. Join the logic of selecting photos from the albums, code such as the following see:

 PackageCom.example.selectfromgallery;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.database.Cursor;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.provider.MediaStore;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public  class mainactivity extends Activity {    PrivateButton Choosefromalbum;PrivateImageView Picimageview;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        picimageview= (ImageView) Findviewbyid (R.id.view);        Choosefromalbum = (Button) Findviewbyid (r.id.choose_from_album); Choosefromalbum.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stubSelectPicture ();    }        }); }/** * Select photos from albums (not cut) */      Private void selectpicture() {//TODO auto-generated method stubIntent intent=NewIntent (); Intent.setaction (Intent.action_pick);//pick an item from the dataIntent.settype ("image/*");//Select from all picturesStartactivityforresult (Intent,1); }@Override     Public void Onactivityresult(intRequestcode,intResultCode, Intent data) {if(RESULTCODE==RESULT_OK) {//Select photos from albums without cutting               Try{Uri selectedimage = Data.getdata ();//Gets the URI of the photo returned by the systemString[] Filepathcolumn = {MediaStore.Images.Media.DATA}; Cursor cursor =getcontentresolver (). Query (SelectedImage, Filepathcolumn,NULL,NULL,NULL);//Query the corresponding photo of the specified URI from the system tableCursor.movetofirst ();intColumnIndex = Cursor.getcolumnindex (filepathcolumn[0]); String PicturePath = cursor.getstring (columnindex);//Get photo pathCursor.close ();                     Bitmap bitmap= Bitmapfactory.decodefile (PicturePath);                 Picimageview.setimagebitmap (bitmap); }Catch(Exception e) {//TODO Auto-generatedcatch blockE.printstacktrace (); }              }Super. Onactivityresult (Requestcode, ResultCode, data); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }@Override     Public Boolean onoptionsitemselected(MenuItem Item) {//Handle Action Bar item clicks here. The Action Bar would        //automatically handle clicks on the Home/up button, so long        //As you specify a parent activity in Androidmanifest.xml.        intid = item.getitemid ();if(id = = r.id.action_settings) {return true; }return Super. onoptionsitemselected (item); }}

parsing:
From mainactivity you can find that selecting a picture from the album is only two steps:
Step one: Send the intent of selecting photos to the system.

 /** * 从相冊选择照片(不裁切) */ private void selectPicture() { // TODO Auto-generated method stub  Intent intent=new Intent(); intent.setAction(Intent.ACTION_PICK);//Pick an item from the data  intent.setType("image/*");//从全部图片中进行选择  startActivityForResult(intent, 1); } 

Step Two: Handle the results returned by the Onactivityresult system.

 Public void Onactivityresult(intRequestcode,intResultCode, Intent data) {if(RESULTCODE==RESULT_OK) {//Select photos from albums without cutting               Try{Uri selectedimage = Data.getdata ();//Gets the URI of the photo returned by the systemString[] Filepathcolumn = {MediaStore.Images.Media.DATA}; Cursor cursor =getcontentresolver (). Query (SelectedImage, Filepathcolumn,NULL,NULL,NULL);//Query the corresponding photo of the specified URI from the system tableCursor.movetofirst ();intColumnIndex = Cursor.getcolumnindex (filepathcolumn[0]); String PicturePath = cursor.getstring (columnindex);//Get photo pathCursor.close ();                     Bitmap bitmap= Bitmapfactory.decodefile (PicturePath);                 Picimageview.setimagebitmap (bitmap); }Catch(Exception e) {//TODO Auto-generatedcatch blockE.printstacktrace (); }              }Super. Onactivityresult (Requestcode, ResultCode, data); }

The system returns a select photo of Uri,uri selectedimage = Data.getdata () and then defines string[] Filepathcolumn = {MediaStore.Images.Media.DATA}; Finds the specified URI path in Filepathcolumn.

by Cursor.getstring (columnindex); Gets the absolute path of the photo.

Demo Download:
http://download.csdn.net/detail/zhoubin1992/8895633

Android Development-Choose a picture from the album without clipping

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.