gpt4 book ai didi

android - 从图库/相机/DropBox 等中选择图像

转载 作者:行者123 更新时间:2023-11-29 14:32:10 26 4
gpt4 key购买 nike

如何让用户选择从Camera/Gallery/DropBox 或设备的任何其他文件系统中选择图像并显示它在 Activity 中作为 ImageView 对象。

最佳答案

要从相机/图库/DropBox 或设备中的任何其他文件系统中选取图像,只需调用隐式 Intent ...

以下代码可能会帮助您...

pickbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
if (Environment.getExternalStorageState().equals("mounted")){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Picture:"), Constants.PICK_IMAGE_FROM_LIBRARY);
}
}
});

现在使用 OnActivity 结果获取数据...

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

if(requestCode == Constants.PICK_IMAGE_FROM_LIBRARY)
{
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
mImagePath = selectedImagePath;
Bitmap photo = getPreview(selectedImagePath);
mImageViewProfileImage.setImageBitmap(photo);
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

public Bitmap getPreview(String fileName)
{
File image = new File(fileName);

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);

if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
{
return null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight : bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / 64;
return BitmapFactory.decodeFile(image.getPath(), opts);
}
}

关于android - 从图库/相机/DropBox 等中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com