Java 将ActivityResult上的数据发送到另一个activity的有效方法

Java 将ActivityResult上的数据发送到另一个activity的有效方法,java,android,android-activity,Java,Android,Android Activity,所以我想从gallery加载一个覆盖onActivityResult的图像。向其他活动发送意图数据的有效方法是什么 当前正在使用此代码获取图像路径,然后再将其发送到其他活动: protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data);

所以我想从gallery加载一个覆盖onActivityResult的图像。向其他活动发送意图数据的有效方法是什么

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

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);

    }
}

问题在于,此代码似乎无法正常工作,因为从gallery应用程序中选择图像后,gallery会延迟几秒钟,然后再将图像加载到其他活动中。

尝试此操作,以下是从相机和gallery获取图像的方法:

    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;
    }

尝试此操作,以下内容用于从相机和多媒体资料中获取图像:

    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;
    }

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

            if (resultCode == RESULT_OK) {
                switch (requestCode) {

                    // Gallery
                    case Keys.PICTURE_REQ_CODE:

                        Uri fileUri = data.getData();
                        cropIntent = new Intent(this,CropActivity.class);
                         cropIntent.setData(fileUri);
                       startActivityForResult(cropIntent,Keys.CROP_REQ_CODE);
                        break;
                }

            }
        }

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

            if (resultCode == RESULT_OK) {
                switch (requestCode) {

                    // Gallery
                    case Keys.PICTURE_REQ_CODE:

                        Uri fileUri = data.getData();
                        cropIntent = new Intent(this,CropActivity.class);
                         cropIntent.setData(fileUri);
                       startActivityForResult(cropIntent,Keys.CROP_REQ_CODE);
                        break;
                }

            }
        }

那么,我是否必须在MainActivity中加载图像,然后将其位图发送到ImageActivity?您可以从gallery中加载图像,将位图转换为base64并将其传递到所需的任何活动。那么,我是否必须在MainActivity中加载图像,然后将其位图发送到ImageActivity?您可以从gallery中加载图像,将位图转换为base64并将其传递给所需的活动。