FileOutputStream не сохранит фотографию из URI - PullRequest
0 голосов
/ 15 апреля 2019

Я пытаюсь взять изображение из галереи и сохранить его в частном месте в моем приложении для другого использования.Я могу подтвердить, что я получаю растровое изображение из галереи (я могу установить изображение в рамках действия с ним), но изображение не может быть доступно в других действиях.Я полагаю, что я делаю что-то не так с моим FileOutputStream.Любые идеи?

Uri photoURI = data.getData();
            Bitmap imageBitmap;

            //Full Size
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            try {
                imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoURI);
                bitmapOptions.inJustDecodeBounds = true;

                //Compress
                ByteArrayOutputStream photoStream = new ByteArrayOutputStream();

                imageBitmap.compress(Bitmap.CompressFormat.JPEG, 50,photoStream);


                picPreview.setImageBitmap(imageBitmap);

                //Save to DB

                FileOutputStream fileOutputStream = new FileOutputStream(photoFile);
                fileOutputStream.write(photoStream.toByteArray());
                fileOutputStream.flush();
                fileOutputStream.close();

            }catch (IOException ex) {
                // Error occurred while creating the File
                Toast.makeText(
                        getApplicationContext(),
                        R.string.error_photo_file,
                        Toast.LENGTH_LONG).show();
            }

Вот функция, где вызывается намерение и где создается «PhotoFile».

   private void ChoosePictureIntent(){
        try {
            photoFile = createImageFile();
            //Save path
            wordPhotoDIR = photoFile.getAbsolutePath();

            thumbnailFile = createImageFile();
            //Save path
            ThumbnailDIR = thumbnailFile.getAbsolutePath();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Toast.makeText(
                    getApplicationContext(),
                    R.string.error_photo_file,
                    Toast.LENGTH_LONG).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.roomwordsample.fileprovider",
                    photoFile);

            Intent ChoosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(ChoosePictureIntent, REQUEST_IMAGE_CHOOSE);
        }
    }
...