Android Development-select pictures from albums not cropped

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 then displayed to the screen. But after the run will be found from the album Selected pictures, no popup cropping interface, directly back.

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 thought it was a problem to send a selection of photos to the system. I am curious to see the next system of Library application (gallery) Source 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 jumps to the cropping interface after modification.
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 correct for us to set up on the imagevew or upload to the network.

Demo:
I am here to give a photo from the album to choose not to crop the demo, tell everyone how to get the absolute path, perfect operation.
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 this:

<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 modify the code in Mainactivity, add the logic to select photos from the album, and the Code looks like this:

 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) {//Choose 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 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 a photo album is only two steps in this feature:
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) {//Choose 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 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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Development-select pictures from albums not cropped

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.