Чтобы загрузить и сохранить изображение с помощью Glide, используйте следующий код:
Glide.with(this)
.as(byte[].class)
.load(photoPath)
.apply(new RequestOptions()
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.format(DecodeFormat.PREFER_ARGB_8888))
.into(new SimpleTarget<byte[]>() {
@Override
public void onResourceReady(@NonNull byte[] resource, @Nullable Transition<? super byte[]> transition) {
new SaveTask(new SaveTask.SimpleListener() {
@Override
public void onPostExecute(File savedFile) {
if (getActivity() == null) return;
CustomToast.showSuccess(getActivity(), R.string.image_saved_in_gallery);
}
}, "jpg").execute(resource);
}
});
SaveTask.java
public class SaveTask extends AsyncTask<byte[], Void, File> {
private Listener mListener;
private String mExtension;
public SaveTask(Listener listener, String extension) {
mListener = listener;
mExtension = extension != null && !extension.isEmpty() ? extension : "jpg";
}
@Override
protected void onPreExecute() {
if (mListener != null) mListener.onPreExecute();
}
@Override
protected File doInBackground(byte[]... params) {
File sdcard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(String.format("%s%s%s%sIMG_%s.%s", sdcard, File.separator, MainApplication.getAppContext().getString(R.string.app_name), File.separator, new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date()), mExtension));
File dir = file.getParentFile();
try {
if (!dir.mkdirs() && (!dir.exists() || !dir.isDirectory())) {
throw new IOException("Cannot ensure parent directory for file " + file);
}
BufferedOutputStream s = new BufferedOutputStream(new FileOutputStream(file));
s.write(params[0]);
s.flush();
s.close();
return file;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(File savedFile) {
if (mListener != null) mListener.onPostExecute(savedFile);
if (savedFile == null) return;
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(savedFile));
MainApplication.getAppContext().sendBroadcast(intent);
}
public interface Listener {
void onPreExecute();
void onPostExecute(File savedFile);
}
public static class SimpleListener implements Listener {
public void onPreExecute() {}
public void onPostExecute(File savedFile) {}
}
}
Не стесняйтесь изменять сохранение файлаПуть как хочешь.