gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:23:02 24 4
gpt4 key购买 nike

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

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

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

1.消息适配器。在聊天 Activity 中使用

 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//给他们一个大图像。

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

    最佳答案

    尝试使用FileProvider.getUriForFile。遵循以下思路的事情应该有望发挥作用。

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

    ( reference )

    关于java - 如何从缓存文件的路径获取文件扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56918849/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com