我是靠谱客的博主 温暖台灯,最近开发中收集的这篇文章主要介绍android studio获取自选图片,从android studio中的图库中选择一张图片?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

这对我有用.

private final static int SELECT_PHOTO = 12345;

imagePick.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

photoPickerIntent.setType("image/*");

startActivityForResult(photoPickerIntent, SELECT_PHOTO);

}

});

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

// Here we need to check if the activity that was triggers was the Image Gallery.

// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.

// If the resultCode is RESULT_OK and there is some data we know that an image was picked.

if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {

// Let's read picked image data - its URI

Uri pickedImage = data.getData();

// Let's read picked image path using content resolver

String[] filePath = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);

cursor.moveToFirst();

String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

BitmapFactory.Options options = new BitmapFactory.Options();

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

imageView.setImageBitmap(bitmap);

// Do something with the bitmap

// At the end remember to close the cursor or you will end with the RuntimeException!

cursor.close();

}

}

最后

以上就是温暖台灯为你收集整理的android studio获取自选图片,从android studio中的图库中选择一张图片?的全部内容,希望文章能够帮你解决android studio获取自选图片,从android studio中的图库中选择一张图片?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部