4

在调用照片库意图后,我使用以下方法获取图像。我想通过传递该图像的位图并在第二个活动中显示该图像来开始另一个活动。但是,从照片库中挑选照片后,什么都不会发生。

protected void onActivityResult(int requestCode, int resultCode, Intent
                    data) {
        final String path;

        if (requestCode == GALLERY_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {

        Uri imageFileUri = data.getData();

            if (imageFileUri != null) {
                try {
                path = getPath(imageFileUri);
                BitmapFactory.Options load_option = new BitmapFactory.Options();
                load_option.inPurgeable = true;
                load_option.inDensity = 0;
                load_option.inTargetDensity = 0;
                load_option.inDensity = 0;
                load_option.inScaled = false;
                bmp_main = BitmapFactory.decodeFile(path, load_option);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp_main.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                Intent intentPhoto = new Intent(MainActivity.this, SecondActivity.class);
                intentPhoto.putExtra("image",byteArray);
                startActivity(intentPhoto);

                    } catch (Exception e) {

                    e.printStackTrace();
                    }
                }
            }

            }


        }

任何人都可以告诉什么问题?

注意:1.我已经在清单中添加了活动

2.没有关于此的logcat错误或异常

3.我已经完成调试,它正确地上升到startActivity行,但之后没有任何反应

4

2 回答 2

1

使用下面的代码...

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri contentUri = data.getData();
        startActivity(new Intent(this, ViewGallery_Photo.class)
                .setData(contentUri));
    }

所以在 onActivityResult 有2个逻辑

1)如果您从画廊加载图像,那么它将带您到其他活动

2)如果您正在从相机捕获图像,那么它将带您到其他活动,而不是画廊调用的活动......

于 2013-12-20T12:50:38.563 回答
1

从你的if条件中调用它:

Intent intentPhoto = new Intent(MainActivity.this, SecondActivity.class);
intentPhoto.putExtra("image",byteArray);
startActivity(intentPhoto);

试试这个:

         try {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            String fullPath = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();
            try {

                File dir = new File(fullPath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                OutputStream fOut = null;
                File file = new File(fullPath, "userImage" + ".png");
                if (file.exists())
                    file.delete();
                file.createNewFile();
                fOut = new FileOutputStream(file);
                fOut.flush();
                fOut.close();
                Log.v("Image saved", "in" + file);
            } catch (Exception e) {
                Log.e("saveToExternalStorage()", e.getMessage());
            }

            decodeFile(picturePath);
            /*
             * iv_display.setImageBitmap(mPhoto); Bitmap useThisBitmap =
             * Bitmap.createScaledBitmap(mPhoto, mPhoto.getWidth(),
             * mPhoto.getHeight(), true);
             */

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            bytepicture = baos.toByteArray();

            Intent newdata = new Intent(MainMenu.this, SecondActivity.class);
            newdata.putExtra("picture", bytepicture);
            startActivity(newdata);
        } catch (Exception e) {
            // TODO: handle exception
            Log.v("TAG", "No Image Selected:");
        }

对于解码文件:

 public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 3;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    mPhoto = BitmapFactory.decodeFile(filePath, o2);
    myBitmap = ExifUtils.rotateBitmap(filePath, mPhoto);

    // image.setImageBitmap(bitmap);
}
于 2013-12-20T12:56:34.387 回答