从图库/相机中选择位图图像后应用程序崩溃(可能是缩放解决方案)

时间:2015-01-21 09:54:58

标签: java android bitmap

我有一个带有图像视图的应用程序,当我点击它时会打开选择Take From Gallery / Open Camera的菜单。 在我选择大图像(大尺寸或大尺寸)后,应用程序崩溃了。 我该怎么办呢? 这是代码和logcat:

选择图片+ onActivityResult:

private void selectImage() {

    final CharSequence[] options = { "Take Photo", "Choose From Gallery",
            "Cancel" };
    AlertDialog.Builder builder = new AlertDialog.Builder(
            AddContactActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(options, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo"))

            {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(intent, 1);

            } else if (options[item].equals("Choose From Gallery")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);

            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            File f = new File(Environment.getExternalStorageDirectory()
                    .toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);

                imginAdd.setImageBitmap(bitmap);
                ByteArrayOutputStream stream3 = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream3);
                byteArray = stream3.toByteArray();

                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System
                        .currentTimeMillis()) + ".jpg");
                try {
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {

                e.printStackTrace();

            }
        } else if (requestCode == 2) {
            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getContentResolver().query(selectedImage, filePath,
                    null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            Bitmap bitmap = (BitmapFactory.decodeFile(picturePath));
            Log.w("path of image from gallery......******************.........",
                    picturePath + "");
            imginAdd.setImageBitmap(bitmap);
            ByteArrayOutputStream stream4 = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream4);
            byteArray = stream4.toByteArray();

        }

    }

}

通过sql从另一个获取图像: (4行作为一个代码,我引用问题:)

  

Blockquote byte [] photo = rs.getBlob(rs                   .getColumnIndex(DBHelper.CONTACTS_COLUMN_IMAGE));   ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);

    Bitmap theImage = BitmapFactory.decodeStream(imageStream);

    imginView.setImageBitmap(theImage);

LOGCAT:http://textuploader.com/69on

1 个答案:

答案 0 :(得分:0)

只需使用此功能即可解码大图像。

private void setPic(final String path) {
    int targetW = imageView.getWidth();
    int targetH = imageView.getHeight();

    final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;
    bitmap = BitmapFactory.decodeFile(path, bmOptions);
}

现在从图库

中检索图像路径后调用此函数
 setPic(imagepath);
 imageView.setImageBitmap(bitmap); // this bitmap object will be declare globally