本地图片选择(打开媒体库,选择图片)
2016-02-23 10:39
183 查看
import java.io.FileNotFoundException; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class Lesson_01_Pic 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.b01); button.setText("选择图片"); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(); /* 开启Pictures画面Type设定为image */ intent.setType("image/*"); /* 使用Intent.ACTION_GET_CONTENT这个Action */ 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()); ContentResolver cr = this.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); ImageView imageView = (ImageView) findViewById(R.id.iv01); /* 将Bitmap设定到ImageView */ imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { Log.e("Exception", e.getMessage(),e); } } super.onActivityResult(requestCode, resultCode, data); } }
相关文章推荐
- 前端架构
- FZU - 1894选拔志愿者【单调队列】
- 使用xtrabackup对MySQL进行备份和恢复
- c/c++内存机制(一)
- adb shell 访问真机手机数据库文件
- nagios setup
- Replacement(思维题)
- 打包程序时的证书问题(上传APP就出现Missing iOS Distribution signing indetity for)
- spring项目中监听器作用-ContextLoaderListener
- Workerman WebSocket示例
- 无废话ExtJs 入门教程十七[列表:GridPanel]
- UNREAL® ENGINE END USER LICENSE AGREEMENT
- Linux Mint 开发者被批评不专业
- 1024. Palindromic Number (25)
- 电商中台订单集成
- 关于基础和软硬件配合的简单思考
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令
- 【Android】【Other】Android一些常用命令