【问题标题】:How to get file extension from path of cached file?如何从缓存文件的路径中获取文件扩展名?
【发布时间】:2019-07-07 01:51:03
【问题描述】:

我正在尝试保存缓存在 Glide 中的文件以进行高效操作。

我得到了缓存文件的路径,它在图像视图中显示没有问题。

我需要缓存文件的扩展名来保存它。 至少我是这样使用的。

1.消息适配器。在 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();
        }
    }

【问题讨论】:

    标签: java android


    【解决方案1】:

    尝试使用FileProvider.getUriForFile。 以下内容应该有望奏效。

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

    (reference)

    【讨论】:

    • 相同..ㅠㅠ 文件扩展名 0 content://com.devdev.Chatchat/cache/image_manager_disk_cache/d2ad7c2105907f0d0b3c8709941598bd9e8b49f03b80482354e7b5979cf91b50.0
    • 在这种情况下,我想您可以尝试从 Uri 确定文件扩展名。但这是 [本质上不可靠的] (stackoverflow.com/a/14604637/11016588)。
    • 我已经这样做了。但这似乎是唯一的方法...谢谢
    • 您好,如果有效,请将答案标记为正确(如果您找到更好的选择,请随时添加您自己的答案),干杯。
    猜你喜欢
    • 2022-06-23
    • 2011-12-08
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多