日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

显示android缓存文件,android – 使用ACTION_VIEW在缓存目录中打开文件

發布時間:2025/4/16 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 显示android缓存文件,android – 使用ACTION_VIEW在缓存目录中打开文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我一直在尋找這個,但我無法讓它正常工作.讓我解釋.

我有一個Android應用程序,可以將文件(圖像,文檔,…)保存在緩存目錄中.起初我曾經使用getExternalCacheDir()方法并將它們保存在那里,但因為它應該緩存在沒有SD卡的設備上,所以我必須使用getCacheDir().

當我以前使用getExternalCacheDir()方法時,在這樣的另一個應用程序中打開這些文件是沒有問題的:

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(file), mimetype);

但是在使用getCacheDir()時,這些文件將保存在應用程序沙箱中,并且無法從應用程序外部訪問.所以我用谷歌搜索了它,然后來了** ContentProvider. ContentProvider可以使用外部應用程序打開私有文件.但是當我嘗試實現它時,它不起作用.

package com.myapplication.providers;

import java.io.File;

import java.io.FileNotFoundException;

import android.content.ContentProvider;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.os.ParcelFileDescriptor;

public class FileProvider extends ContentProvider {

@Override

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {

File privateFile = new File(getContext().getCacheDir(), uri.getPath());

return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);

}

@Override

public int delete(Uri arg0, String arg1, String[] arg2) {

return 0;

}

@Override

public String getType(Uri arg0) {

return null;

}

@Override

public Uri insert(Uri arg0, ContentValues arg1) {

return null;

}

@Override

public boolean onCreate() {

return false;

}

@Override

public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,

String arg4) {

return null;

}

@Override

public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {

return 0;

}

}

將此提供程序添加到應用程序清單

android:name="com.myapplication.providers.FileProvider"

android:authorities="com.myapplication"

android:exported="true" />

并使用此代碼打開文件:

Uri uri = Uri.parse("content://com.myapplication/" + filename);

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(uri, mimetype);

提前致謝!

總結

以上是生活随笔為你收集整理的显示android缓存文件,android – 使用ACTION_VIEW在缓存目录中打开文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。