Camera2 api BitmapFactory.decodeStream не создает растровое изображение - PullRequest
0 голосов
/ 06 августа 2020
• 1000 api, и то же самое, он уже работает нормально, но ошибка возникает только на некоторых устройствах, когда я сгенерирую свой InputStream, он кажется нормальным, и после его создания я пытаюсь создать растровое изображение с помощью BitmapFactory.decodeStream (input_stream ), но он не возвращает растровое изображение на телефонах samsung, другие телефоны возвращаются нормально, а фотография сохраняется на моем устройстве, однако, когда я использую samsung, он выдает ошибку.
private InputStream getInputStreamFromContentUri( Uri uri )
    throws Exception
{
    return this.activity.getContentResolver().openInputStream( uri );
}

public Uri resizeBitmap( Uri bitmap_uri )
        throws Exception
{
    this.log( String.format("resizeBitmap with this Uri: %s", bitmap_uri ) );

    InputStream input_stream = getInputStreamFromContentUri( bitmap_uri );
    Bitmap bitmap = BitmapFactory.decodeStream( input_stream );
    input_stream.close();

    if (bitmap == null)
    {
        //him to the app here
        this.log("Bitmap is null");
        return null;
    }

    File image = this.createTemporaryImageFile( 10 );
    FileOutputStream file_output_stream = new FileOutputStream( image );
    input_stream = getInputStreamFromContentUri( bitmap_uri );
    Matrix matrix = rotationCorrection( input_stream );
    input_stream.close();

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float aspect_ratio =  ( ( float ) width / height );
    this.log( aspect_ratio, "Picture AspectRatio" );
    Bitmap resized = bitmap;
    if ( width > Asset.MAX_BITMAP_WIDTH )
    {
        int smaller_height = ( int ) ( Asset.MAX_BITMAP_WIDTH / aspect_ratio );
        resized =
                Bitmap.createScaledBitmap
                        (
                                bitmap, Asset.MAX_BITMAP_WIDTH,
                                smaller_height,
                                false
                        );
        bitmap = Bitmap.createBitmap( resized, 0, 0, Asset.MAX_BITMAP_WIDTH, smaller_height, matrix, true );
    }
    else
    {
        bitmap = Bitmap.createBitmap( resized, 0, 0, width, height, matrix, true );
    }

    bitmap.compress( Bitmap.CompressFormat.JPEG, Asset.JPEG_QUALITY, file_output_stream );
    Uri uri = this.generateUriFromTemporaryImageFile( image );
    return uri;
}

Я сохраняю файл следующим образом:

String uri_image = getIntent().getExtras().getString("img_uri");
        Uri file_uri = Uri.parse(uri_image);

        if (uri_image == null || uri_image.equals("")) {
            file = new File(MainActivity.this.getFilesDir(), "imagem.jpg");
            file_uri = Uri.fromFile(file);
        }

        asset.log("img_uri: " + file_uri.toString());
        picture_file = new File(file_uri.getPath());

ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Image image = null;
                try {
                    image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.remaining()];
                    buffer.get(bytes);

                    save(bytes, picture_file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (image != null) {
                        image.close();
                    }
                }
            }

            private void save(byte[] bytes, File picture) throws IOException {
                try
                {
                    FileOutputStream fos = new FileOutputStream(picture);
                    fos.write(bytes);
                    fos.close();
                    asset.log("getTotalSpace: " + picture.getTotalSpace());
                }
                catch (FileNotFoundException e)
                {
                    asset.log("File not found: " + e.getMessage(), e);
                }
                catch (IOException e)
                {
                    asset.log("Error accessing file: " + e.getMessage(), e);
                }
                catch(Exception e)
                {
                    asset.log("Error: " + e.getMessage(), e);
                }
            }
        };
...