gpt4 book ai didi

java - 将 ActivityResult 数据发送到另一个 Activity 的有效方法

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:23 25 4
gpt4 key购买 nike

所以我想通过重写 onActivityResult 从图库加载图像。将 Intent 数据发送到另一个 Activity 的有效方法是什么?

当前使用此代码来获取图像路径,然后再将其发送到其他 Activity :

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);

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

Uri selected_image = data.getData();
if (selected_image.toString().substring(0, 21).equals("content://com.android")) {
String[] photo_split = selected_image.toString().split("%3A");
String imageUriBasePath = "content://media/external/images/media/" + photo_split[1];
selected_image = Uri.parse(imageUriBasePath);

}
String[] file_path_column = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selected_image, file_path_column, null, null, null);
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(file_path_column[0]);
image_path = cursor.getString(column_index);
cursor.close();

Intent intent = new Intent(MainActivity.this, ImageActivity.class);
intent.putExtra("imagePath", image_path);
startActivity(intent);

}
}

问题是此代码似乎不能很好地工作,因为从图库应用程序中选择图像后,图库会在将图像加载到其他 Activity 之前滞后几秒钟。

最佳答案

试试这个,以下是从相机和图库获取图像:

    ImageView photo;
Bitmap bmp;
static int GET_PICTURE = 1, CAMERA_PIC_REQUEST = 2;
static String selectedImagePath = "";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
// deleteDialog.dismiss();
bmp = (Bitmap) data.getExtras().get("data");
} else if (requestCode == GET_PICTURE) {

if (bmp != null) {
bmp.recycle();

}
// deleteDialog.dismiss();
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
options.inSampleSize = calculateInSampleSize(options, 200,
200);
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(selectedImagePath, options);
}
if (bmp != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

Bitmap outputs = getRoundedBitmap(bmp,10);
last = Bitmap.createScaledBitmap(outputs, 200, 200, false);
if (last != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
last.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);

photo.setImageBitmap(last);
// userimage.setBackgroundResource(android.R.color.transparent);
}

} else {
Toast.makeText(getBaseContext(), "Invalid image",
Toast.LENGTH_SHORT).show();
}
}
}

public static Bitmap getRoundedBitmap(Bitmap bitmap,int pixels) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null,
null, null);
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

result = cursor.getString(idx);

}
cursor.close();
return result;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}

关于java - 将 ActivityResult 数据发送到另一个 Activity 的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31656169/

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