从Gallery(图库)选择一张图片到界面上
1、启动系统图库应用 ①启动图库 START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery/com.android.camera.GalleryPicker u=0} from pid 264 ②选取图片 START {act=android.intent.action.VIEW dat=content://media/internal/images/media?bucketId=-1595679508 cmp=com.android.gallery/com.android.camera.ImageGallery (has extras) u=0} from pid 1485 com.android.camera.ImageGallery 2、查看源码,得知如何写Intent了 <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>
|
核心代码: package cn.zengfansheng.pickPicFromGallary;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) this.findViewById(R.id.iv);
}
/**
* 从图库中选择一张图片
*
* @param view
*/
public void pick(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.setType("image/*");
this.startActivityForResult(intent, 0);
}
// 得到返回值
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
iv.setImageURI(uri);
}
}
} 源码: |
结果: |