将onActivityResult上的数据发送到另一个活动的有效方法

时间:2015-07-27 14:43:04

标签: java android android-activity

所以我想从覆盖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);

    }
}

问题是这段代码看起来效果不好,因为从图库应用程序中选择图像后,图库会在将图像加载到其他活动之前滞后几秒钟。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

// Opening gallery 
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), Keys.PICTURE_REQ_CODE);

// Opening gallery Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(Intent.createChooser(intent, "Select Picture"), Keys.PICTURE_REQ_CODE);