从图库

时间:2016-03-29 10:54:21

标签: android

我使用以下代码来获取所选图像的方向,这样如果它是垂直拍摄的,当从图库中选择时,它将不会水平显示。

错误发生在 File imageFile = new File(selectedImage.toString());在参数selectedImage.toString()中,因为当我更改初始int rotate = 90并选择垂直图像时,结果很好。

我是否将参数传递给文件正确?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    BitmapFactory.Options options;
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Uri selectedImage = data.getData();
            InputStream stream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
            stream.close();
            //orientation
            try {
                int rotate = 0;
                try {
                    File imageFile = new File(selectedImage.toString());
                    ExifInterface exif = new ExifInterface(
                            imageFile.getAbsolutePath());
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Matrix matrix = new Matrix();
                matrix.postRotate(rotate);
                yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);  }
            catch (Exception e) {}
            //end of orientation

            imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgButton.setImageBitmap(yourSelectedImage);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

2 个答案:

答案 0 :(得分:1)

像这样修改你的方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    BitmapFactory.Options options;
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Uri selectedImage = data.getData();

            String[] filePath = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null);
                    cursor.moveToFirst();
            String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            InputStream stream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
            stream.close();
            //orientation
            try {
                int rotate = 0;
                try {
                    ExifInterface exif = new ExifInterface(
                            mImagePath);
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Matrix matrix = new Matrix();
                matrix.postRotate(rotate);
                yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);  }
            catch (Exception e) {}
            //end of orientation

            imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgButton.setImageBitmap(yourSelectedImage);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
    }
}

答案 1 :(得分:0)

用于图像处理编译picasso是使用它的最佳库您可以使用它轻松处理图像,您也可以从缓存加载图像并提高应用程序的性能,因此尽量避免执行大量代码并在构建gradle中添加以下行然后在你的代码中使用picasso。

compile 'com.squareup.picasso:picasso:2.5.0'
相关问题