I'm trying to save a file cached in the Glide for efficient operation.
I got the path to the cached file and it's shown without a problem in the imageview.
I need an extension of the cached file to save it. At least in this way I use it.
1.MessageAdapter. used in the chatActivity
holder.imgHolder.setOnClickListener(new View.OnClickListener() { //using glide
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ImageActivity.class);
intent.putExtra("uri",chat.getImageUri());
mContext.startActivity(intent);
}
});
ImageActivity // to give them a big image.
String cachedPath,fileExtention; Intent intent = getIntent(); imgUri = intent.getStringExtra("uri"); Log.e("glideUri", imgUri);//uri From server. if uri is same, Glide show cached file AsyncTask.execute(new Runnable() { //get cached filePath in backroundThread @Override public void run() { cachedPath = getImgCachePath(imgUri); // retruns /data/user/0/com.devdev.Chatchat/cache/image_manager_disk_cache/c70aa7d94d9648c105a020f85d444182cc85ee298f0c1ec5aebe93c31984041b.0 fileExtention = MimeTypeMap.getFileExtensionFromUrl(cachedPath);//returns 0 Log.e("cachedPath", cachedPath); Log.e("fileExtention", fileExtention); } }); new ClearSpTask(new ClearSpTask.AsynResponse() {//to know if AsyncTask is complete @Override public void processFinish(Boolean output) { Glide.with(ImageActivity.this).load(cachedPath).into(zoomImageView); //works well button_down.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("fileExtention", fileExtention); //returns 0; downImage(cachedPath); //works well if I set fileExtention manually. like jpg,gif Toast.makeText(ImageActivity.this, "Download complete", Toast.LENGTH_SHORT).show(); } }); } }).execute();
getImgCachePath();
private String getImgCachePath(String url) {
FutureTarget<File> futureTarget = Glide.with(getBaseContext()).load(url).downloadOnly(100, 100);
try {
File file = futureTarget.get();
String path = file.getAbsolutePath();
return path;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
downImage();
public void downImage(String cachedUri){
try
{
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "kmChat" + System.currentTimeMillis() + "."+fileExtention);
long startTime = System.currentTimeMillis();
InputStream is =new FileInputStream(cachedUri);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] img = new byte[1024];
int current = 0;
while ((current = bis.read()) != -1) {
baos.write(current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
}