无法从主活动中的onActivityResult方法开始新活动

时间:2013-12-20 12:39:16

标签: android bitmap onactivityresult

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

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

            }


        }

有人可以说出问题是什么吗?

N.B。:1。我已在清单中添加了活动

2。关于此

没有logcat错误或异常

第3。我已经完成了调试,它正确地启动了startActivity,但之后没有发生任何事情

2 个答案:

答案 0 :(得分:2)

使用以下代码......

    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)如果您从相机捕捉图像,那么它将带您进入其他活动,而不是通过图库调用的相同活动......

答案 1 :(得分: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);
}