如何从画廊(SD卡)为我的应用程序选择图片?这个问题最初是针对Android1.6提出的。我正在我的应用程序中处理照片选项。我的活动中有一个按钮和一个ImageView。当我单击该按钮时,它将重定向到图片库,并且我将能够选择一个图像。选定的图像将出现在“我的图像视图”中。
3 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
private static final int SELECT_PHOTO = 100;
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);photoPickerIntent.setType("image/*");startActivityForResult(photoPickerIntent, SELECT_PHOTO);
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case SELECT_PHOTO: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); InputStream imageStream = getContentResolver().openInputStream(selectedImage); Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); } }}
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); // The new size we want to scale to final int REQUIRED_SIZE = 140; // Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) { break; } width_tmp /= 2; height_tmp /= 2; scale *= 2; } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); }
添加回答
举报
0/150
提交
取消