4

フォト ギャラリー インテントを呼び出した後、次のメソッドを使用して画像を取得しました。その画像のビットマップを渡して別のアクティビティを開始し、2 番目のアクティビティでその画像を表示したいと考えています。ただし、フォト ギャラリーから写真を選択した後は何も起こりません。

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 に答える