- 2
- 0
- 约2.69千字
- 约 6页
- 2017-10-16 发布于河南
- 举报
当要进分享照片功能时,点击一个
当要进分享照片功能时,点击一个按钮,然后进入图库选择照片,要进行这样的操作,改怎么完成呢,要解决2个问题:1、该如何进入图库2、改如何取得照片==================================================进入图库:? ? ? ? ? ? ? ? Intent intent = new Intent(); ?? ? ? ? ? ? ? ? intent.setType(image/*); ?? ? ? ? ? ? ? ? intent.setAction(Intent.ACTION_GET_CONTENT); ??? ? ? ? ? ? ? ? /* 取得相片后返回本画面 */ ?? ? ? ? ? ? ? ? startActivityForResult(intent, 1);?选取照片:? ? ? ? ? ? ? ?当点击照片后,会返回一个intent,那么,用getData();获得一个URI的数据,这个uri就是图片在数据库中的位置? ? ? ? ? ? ? ?然后该怎么通过这个uri操作,得到图片和路径呢??看下面源码吧====================================================最后详细的见源码public class StartActivityForResult extends Activity {? ? /** Called when the activity is first created. */? ? @Override? ? public void onCreate(Bundle savedInstanceState) {? ? ? ? super.onCreate(savedInstanceState);? ? ? ? setContentView(R.layout.main);? ? ? ??? ? ? ? Button button = (Button)findViewById(R.id.btn); ?? ? ? ? button.setText(选择图片); ?? ? ? ? button.setOnClickListener(new Button.OnClickListener(){ ?? ? ? ? ? ? @Override ?? ? ? ? ? ? public void onClick(View v) { ?? ? ? ? ? ? ? ?Intent intent = new Intent(); ?? ? ? ? ? ? ? ? intent.setType(image/*); ?? ? ? ? ? ? ? ? intent.setAction(Intent.ACTION_GET_CONTENT); ??? ? ? ? ? ? ? ? /* 取得相片后返回本画面 */ ?? ? ? ? ? ? ? ? startActivityForResult(intent, 1); ??? ? ? ? ? ? } ?? ? ? ? ? ? ??? ? ? ? }); ?? ? } ?? ? ??? ? @Override ?? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) { ?? ? ? ? if (resultCode == RESULT_OK) { ?? ? ? ? ? ? Uri uri = data.getData(); ?? ? ? ? ? ? Log.e(uri, uri.toString());?? ? ? ? ? ? Log.e(uri, uri.getPath()); ?? ? ? ? ? ? ContentResolver cr = this.getContentResolver();??? ? ? ? ? ? //获得照片,并显示? ? ? ? ? ? try { ?? ? ? ? ? ? ? ? Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); ?? ? ? ? ? ? ? ? ImageView imageView = (ImageView) findViewById(R.id.iv); ?? ? ? ? ? ? ? ? /* 将Bitmap设定到ImageView */ ?? ? ? ? ? ? ? ? imageView.setImageBitmap(bitmap); ?? ? ? ? ? ? } catch (FileNotFoundException e) { ?? ? ? ? ? ?
您可能关注的文档
最近下载
- (人教版)数学二年级上册寒假作业-2025年秋季版,30份题组.docx
- 12YJ5-1 平屋面参考图集.docx VIP
- 富邦生物(内蒙古)有限公司年产 500 吨多杀菌素项目环境影响报告书.pdf
- 金宝血滤机Prismaflex操作流程-CRRT.pptx VIP
- 药学专业毕业论文6000字.doc VIP
- 邓丽君经典老歌之不朽名曲专辑.doc VIP
- DB32T4122-2021开发区地质灾害危险性区域评估规范1.pdf VIP
- 光波导仿真:弯曲波导仿真_(5).不同弯曲半径对波导性能的影响.docx VIP
- 有限空间应急物资检查、维护、保养记录.doc VIP
- 《无线网络与移动通讯技术》教学大纲(模板).docx VIP
原创力文档

文档评论(0)