gpt4 book ai didi

android - 从图库应用程序中选择图像

转载 作者:行者123 更新时间:2023-11-30 01:37:58 24 4
gpt4 key购买 nike

我使用此代码允许用户从图库应用中选择一张图片,然后再获取该图片。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
mImg.setImageBitmap(photo);
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(getActivity(), selectedImageUri, projection, null, null,
null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();

String selectedImagePath = cursor.getString(column_index);

Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);

mImg.setImageBitmap(bm);
mImg.setAlpha(1);

}
}
}

但在这个答案中 here有人告诉我这段代码在大多数 Android 设备上都不起作用。我能知道为什么吗?获取用户选择的图像的最佳方式是什么。

我将其发布在一个单独的问题中,因为它可能很有趣。

最佳答案

通过使用位图,您的图像可能看起来很模糊。如果图片太大,当你想上传或检索它们到(从)服务器时会出现问题。所以 uribitmap 好。

你可以尝试下面的代码,你会看到 bitmapuri 之间的区别

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
else
{
imageView.setImageResource(R.mipmap.no_image);
}

break;

case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
Log.e("A", "AAA");
}
}
}

关于android - 从图库应用程序中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880755/

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