/android/data/com.example.app/user/
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());
}
});
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());
}
}
<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"/>
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
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
mContext.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29" />
static public @Nullable
Uri cacheUri(Uri uri, Context context) throws IOException {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
if (inputStream != null) {
java.io.File outputDir = Objects.requireNonNull(context).getCacheDir(); // context being the Activity pointer
java.io.File outputFile = java.io.File.createTempFile("spool_job_", "",outputDir);
BufferedInputStream input = new BufferedInputStream(inputStream);
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
// read and write into the cache directory
byte[] bArr = new byte[8192];
while (true) {
int read = input.read(bArr);
if (read < 0) {
break;
} else {
output.write(bArr, 0, read);
}
}
// close the streams
input.close();
output.close();
return Uri.fromFile(outputFile);
}
return null;
}