0

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);
        }
    });
  1. 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();
        }
    }
mouse1
  • 43
  • 7

1 Answers1

0

Try using FileProvider.getUriForFile. Something along the following lines should hopefully work.

Uri uri = FileProvider.getUriForFile(context, context.getPackageName(), file);

(reference)

Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
  • same.. ㅠㅠ file extention 0 content://com.devdev.Chatchat/cache/image_manager_disk_cache/d2ad7c2105907f0d0b3c8709941598bd9e8b49f03b80482354e7b5979cf91b50.0 – mouse1 Jul 07 '19 at 02:47
  • In that case, I guess you could try to determine the file extension from the Uri. But this is [inherently unreliable] (https://stackoverflow.com/a/14604637/11016588). – Chrisvin Jem Jul 07 '19 at 03:22
  • I was already doing it this way. But this seems to be the only way... Thank you – mouse1 Jul 07 '19 at 03:33
  • Hi, mark the answer as correct if it works (or feel free to add your own answer if you found a better alternative), cheers. – Chrisvin Jem Jul 18 '19 at 08:03