这几天我都在做Android的App,同时进修它的API,我将分享一些我学到的工具,比方: 怎么从系统图库当选择图片。
起首,让我们来看看怎么将手机系统图库集成到你的App中,然后再从图库当选择图片来做一些事。例如,在Facebook的App,你就能够间接选择手机上的图片上传到你的个人材料。
让我们来做一个容易例子,请求:
让我们Start。
步调1:创立根本的Android项目
在Eclipse中,点击New > Project > Android Project,给项目取名为“ImageGalleryDemo”,然后选择Android2.1或sdk 7。
一旦这一步完成,你将看到一个根本的hello world顺序。
步调2:修正结构文件
在我们的例子中,我们需求一个容易的结构:一个ImageView控件来显示我们选中的图片,一个Button控件点击重定向得手机图库。
在项目中翻开layout/main.xml,然后交换成下面的代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imgView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"></ImageView> <Button android:id="@+id/buttonLoadPicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:text="Load Picture" android:layout_gravity="center"></Button> </LinearLayout>
步调3:编写重定向到图片库的代码
如今我们需求写一些Java代码来处置按钮的点击事情,而重定向到图片库的代码以下:
Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE);
留意:这里要传一个整形的常量RESULT_LOAD_IMAGE到startActivityForResult()办法。
步调4:获得选中的图片
一旦用户选择了一张图片,onActivityResult()办法将会被挪用。我们需求处置这个办法失掉的数据,代码以下:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); // String picturePath contains the path of selected Image }
留意:onActivityResult()办法只要当图片被选中后才会挪用。在这个办法中,我们需求反省requestCode能否是我们之前传给startActivityForResult()办法的RESULT_LOAD_IMAGE。
终极代码
ImageGalleryDemoActivity类的终极代码以下:
package net.viralpatel.android.imagegalleray; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class ImageGalleryDemoActivity extends Activity { private static int RESULT_LOAD_IMAGE = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture); buttonLoadImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); ImageView imageView = (ImageView) findViewById(R.id.imgView); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } } }
顺序截图
第一屏:用户将重定向得手机图库
用户从图库选择图片
在我们的App显示用户选中的图片
源代码:ImageGalleryDemo.zip (46 KB)
本文中的一切译文仅用于进修和交换目标,转载请务必注明文章译者、出处、和本文链接。 2KB翻译任务按照 CC 协议,假如我们的任务有进犯到您的权益,请实时联络我们。2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务