Как сохранить Uri в application директория?

Ко мне поступает изображение в виде Uri из Cloud Firebase нужно сохранить его в памяти телефона, желательно по пути

/android/data/com.example.app/user/

Coud Firebase sdk код

 StorageReference storageReference;
        storageReference = FirebaseStorage.getInstance().getReference();

        storageReference.child("users").child("images")
                .getDownloadUrl()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful() && task.getResult() != null) {
                        Uri uri = task.getResult();
                        listenerUri.OnSuccessListener(uri);

                        saveUri(context, uri);
                        Log.w("SUCCESS", "Image Loaded-> " +task.getResult().getPath());
                    }else {
                        listenerUri.OnFailureListener(task.getException());
                    }
                });

Метод saveUri которые принимает Uri чтобы сохранить его в памяти устройство

 public void saveUri(Context context, Uri uri){

        try {

            InputStream inputStream = context.getContentResolver().openInputStream(uri);

            // Get application directory
            String appPath = context.getApplicationInfo().dataDir;

            // Create new file in application directory
            FileOutputStream outputStream = new FileOutputStream(appPath+"/user/simple.jpg");

            // Break progress
            if (inputStream == null){
                Log.w("SaveUri", "Error: inputStream is null");
                return;
            }

            // Initialize the conversion buffer byte array.
            byte[] conversionBufferByteArray = new byte[1024];

            // Initialize loading counter.
            long load = 0;

            // Define the buffer length variable.
            int bufferLength;

            // Attempt to read data from the input stream and store it in the output stream.  Also store the amount of data read in the buffer length variable.
            while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) {  // Proceed while the amount of data stored in the buffer in > 0.

                // Write the contents of the conversion buffer to the file output stream.
                outputStream.write(conversionBufferByteArray, 0, bufferLength);

                // Update the downloaded kilobytes counter.
                load = load + bufferLength;

                // Update the file download progress.
                Log.d(TAG, "Loading: "+load+"%");
            }

            // Close the input stream.
            inputStream.close();
            // Close the output stream.
            outputStream.close();

        }catch (IOException exception) {
            Log.e("SaveUri", "Error: "+exception.getMessage());
        }

    }

разрешения на запись и считывания данные имеется !

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />

Рузультат ...

E/SaveUri: Error: No content provider: https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/items%2F1653978775982_0.jpg?alt=media&token=881afe12-46fa-4a97-90ed-cbc28d2d9fd3
W/SUCCESS: Image Loaded-> /v0/b/example.appspot.com/o/items/1653978775982_0.jpg

Изображенте загружается успешно но не сохраняется ! что не так с моим кодом ? Thank you


Ответы (0 шт):