причина, по которой вы получаете расширение .0
, заключается в том, что downloadOnly
предоставляет вам файл из кэша SOURCE, а DiskLruCache имеет встроенную версию. В зависимости от того, что вы делаете с selectedPhotoPath, я предлагаю скопировать (не переименовывать) его на конечное место (вы можете получить тип содержимого или имя файла из photoUri) или сохранить его в памяти с помощью: вы можете копировать (не переименовывать) его на конечное место (вы можете получить тип содержимого или имя файла из photoUri) или сохранить его в памяти. Вот код, который может сохранить изображение (оно в java, но я уверен, что вы можете преобразовать его в kotlin):
public static synchronized void saveImage(final Context context, final String imageUrl) {
Glide.with(context.getApplicationContext())
.load(imageUrl)
.downloadOnly(new SimpleTarget<File>() {
@Override public void onResourceReady(File src, GlideAnimation<? super File> glideAnimation) {
new GalleryFileSaver(context, imageUrl, src).execute();
}
});
}
private static class GalleryFileSaver extends AsyncTask<Void, Void, File> {
private final Context context;
private final String imageUrl;
private final File src;
public GalleryFileSaver(Context context, String imageUrl, File src) {
this.context = context;
this.imageUrl = imageUrl;
this.src = src;
}
@Override protected File doInBackground(Void... params) {
try {
dst = new File(getGalleryFolder(), getTargetFileName());
copy(new FileInputStream(src), new FileOutputStream(dst));
} catch (IOException e) {
dst = null;
e.printStackTrace();
}
return dst;
}
@Override protected void onPostExecute(File dst) {
String message = dst != null? "Saved file to " + dst : "Failed to save file from " + src;
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
private void copy(InputStream in, OutputStream out) throws IOException {
try {
byte[] buf = new byte[1024 * 16];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
try { in.close(); } catch (IOException ignore) { }
try { out.close(); } catch (IOException ignore) { }
}
}
private String getTargetFileName() throws IOException {
Random random = new Random();
String ext = android.webkit.MimeTypeMap.getFileExtensionFromUrl(imageUrl);
String name = String.valueOf(random.nextInt()) + ".jpg";
Log.w("saeed", name);
return name;
}
private File getGalleryFolder() throws IOException {
File gallery = new File(APP_GALLERY_IMAGE_PATH);
if (!gallery.mkdirs() && (!gallery.exists() || !gallery.isDirectory())) {
throw new IOException("Invalid gallery path: " + gallery);
}
return gallery;
}
}