android raw获取uri,从资产或res / raw中的文件中获取Uri

I have tried to get this working and I have looked at many different resources online (as you can see from all of the comments I have made). I want to access a .pdf file that is either located in assets or res; It does not matter to which one so the easiest way will do.

I have the method below that will get the actual file and will call another method(under the first method below) with the Uri in the parameters.

Thank you very much for your help and I will be standing by to answer questions or add more content.

private void showDocument(File file)

{

ORIGINAL

//showDocument(Uri.fromFile(file));

//

// try 1

//File file = new File("file:///android_asset/RELATIVEPATH");

// try 2

//Resources resources = this.getResources();

// try 4

String PLACEHOLDER= "file:///android_asset/example.pdf";

File f = new File(PLACEHOLDER);

//File f = new File("android.resource://res/raw/slides1/example.pdf");

//getResources().openRawResource(R.raw.example);

// try 3

//Resources resources = this.getResources();

//showDocument(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(R.raw.example) + '/' + resources.getResourceTypeName(R.raw.example) + '/' + resources.getResourceEntryName(R.raw.example)));

showDocument(Uri.fromFile(f));

}

protected abstract void showDocument(Uri uri);

解决方案

sing the resource id, the format is:

"android.resource://[package]/[res id]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

or, using the resource subdirectory (type) and resource name (filename without extension), the format is:

"android.resource://[package]/[res type]/[res name]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取文件 URI 的方法取决于文件的来源和您的应用程序的运行环境。以下是几种获取文件 URI 的常见方法: 1. 从本地存储器获取文件 URI: ```java File file = new File(Environment.getExternalStorageDirectory(), "example.txt"); Uri uri = Uri.fromFile(file); ``` 2. 从应用程序资源获取文件 URI: ```java Uri uri = Uri.parse("android.resource://com.example.myapp/raw/example"); ``` 3. 从 ContentProvider 获取文件 URI: ```java Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues()); ``` 上述方法仅适用于 Android 7.0 及以下版本。从 Android 7.0 开始,您需要使用 FileProvider 来安全地共享文件 URI。使用 FileProvider 可以确保您的应用程序只能访问您明确授权的文件。 以下是使用 FileProvider 获取文件 URI 的示例代码: ```xml <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.myapp.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` ```java File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "example.jpg"); Uri uri = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", file); ``` 请注意,您需要在 AndroidManifest.xml 文件注册 FileProvider,并在 res/xml 文件创建 file_paths.xml 文件来指定要共享的文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值