获取资产文件夹中文件的内容URI

时间:2019-09-13 07:30:58

标签: java android

我的资产文件夹中有一个zip文件,我想要一个内容uri。

我可以使用以下方法获取文件uri:

final Uri uri = Uri.fromFile(new File("file:///android_asset/xyz.zip"));

对于内容uri,我尝试使用:

final Uri uri = Uri.parse("content://com.example.myproject/xyz.zip");

但是它不起作用吗?

1 个答案:

答案 0 :(得分:0)

尝试此代码

InputStream inputStream = am.open("myfoldername/myfilename");
File file = createFileFromInputStream(inputStream);
final Uri uri = Uri.fromFile(file);

private File createFileFromInputStream(InputStream inputStream) {
try{
  File f = new File(my_file_name);
  OutputStream outputStream = new FileOutputStream(f);
  byte buffer[] = new byte[1024];
  int length = 0;

  while((length=inputStream.read(buffer)) > 0) {
    outputStream.write(buffer,0,length);
  }

  outputStream.close();
  inputStream.close();

  return f;
 }catch (IOException e) {
     //Logging exception
 }

return null;
}