Android-打开相册选择单张图片

xiaoxiao2021-02-28  78

btn8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /*在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量, // 该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量, //然后设置类型为“image/*”,就可获得Android手机内的所有image。*/ Intent intent = new Intent(); /* 开启Pictures画面Type设定为image */ intent.setType("image/*"); /* 使用Intent.ACTION_GET_CONTENT这个Action */ intent.setAction(Intent.ACTION_GET_CONTENT); /* 取得相片后返回本画面 */ startActivityForResult(intent, 1); //Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // startActivityForResult(intent, RESULT); } }); @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)); /* 将Bitmap设定到ImageView */ iv8.setImageBitmap(bitmap); } catch (FileNotFoundException e) { Log.e("Exception", e.getMessage(), e); } } super.onActivityResult(requestCode, resultCode, data); }
转载请注明原文地址: https://www.6miu.com/read-29429.html

最新回复(0)