Example usage for android.graphics ImageDecoder createSource

List of usage examples for android.graphics ImageDecoder createSource

Introduction

In this page you can find the example usage for android.graphics ImageDecoder createSource.

Prototype

@AnyThread
@NonNull
public static Source createSource(Resources res, InputStream is) 

Source Link

Document

Internal API used to generate bitmaps for use by Drawables (i.e.

Usage

From source file:java_lang_programming.com.android_media_demo.article94.java.ImageDecoderActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case (REQUEST_CODE_CHOOSER):
        if (resultCode != RESULT_OK || data.getData() == null) {
            Toast.makeText(this, getString(R.string.image_unselected_message), Toast.LENGTH_LONG).show();
            return;
        }/*ww w  . j av a  2s .  com*/
        Bitmap bitmap = null;
        // P ?
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {
            Log.d("ImageDecoderActivity", "ImageDecoder");
            StringBuilder msg = new StringBuilder();
            try {
                //                        bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), data.getData()));
                bitmap = ImageDecoder.decodeBitmap(
                        ImageDecoder.createSource(getContentResolver(), data.getData()),
                        new ImageDecoder.OnHeaderDecodedListener() {
                            @Override
                            public void onHeaderDecoded(ImageDecoder imageDecoder,
                                    ImageDecoder.ImageInfo imageInfo, ImageDecoder.Source source) {
                                if (checkedResize.isChecked()) {
                                    Size size = imageInfo.getSize();
                                    imageDecoder.setResize(size.getWidth() * 2, size.getHeight() * 2);
                                }

                                if (checkedCrop.isChecked()) {
                                    Size size = imageInfo.getSize();
                                    imageDecoder.setCrop(new Rect(0, 0, size.getWidth(), size.getHeight() / 2));
                                }

                                msg.append("ImageDecoder?bitmap??????\n");
                                msg.append("? : " + imageInfo.getSize() + "\n");
                                msg.append("? : " + imageInfo.getMimeType() + "\n");
                                msg.append(" : " + imageInfo.isAnimated() + "\n");
                            }
                        });
            } catch (IOException e) {
                e.printStackTrace();
            }
            selectedImageInfo.setText(msg.toString());
            // ?
        } else {
            Log.d("ImageDecoderActivity", "BitmapFactory");
            bitmap = getBitmap(getApplicationContext(), data.getData());
            selectedImageInfo.setText("BitmapFactory?bitmap??????");
        }

        if (bitmap != null) {
            selectedImage.setImageBitmap(bitmap);
        }
        break;
    default:
        break;
    }
}