如何从缓存文件的路径获取文件扩展名?

时间:2019-07-07 01:51:03

标签: java android

我正在尝试保存在Glide中缓存的文件,以实现高效操作。

我获得了缓存文件的路径,并且在imageview中显示该文件没有问题。

我需要缓存文件的扩展名来保存它。 至少我以这种方式使用它。

1.MessageAdapter。在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 //为他们提供大图像。

    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();
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用FileProvider.getUriForFile。 希望可以遵循以下内容。

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

reference