【问题标题】:android: avoid file download with MODE_WORLD_READABLEandroid:避免使用 MODE_WORLD_READABLE 下载文件
【发布时间】:2013-05-01 07:33:01
【问题描述】:

你好

我想为 android 创建一个简单的市场应用程序,但我遇到了一个烦人的问题。

安装流程如下:

1- 在应用程序上下文中下载 apk 文件:

  InputStream input = new BufferedInputStream(url.openStream()) ;
  OutputStream output = openFileOutput("xx.apk", Activity.MODE_WORLD_READABLE);

  byte data[] = new byte[1024];

  while ((count = input.read(data)) != -1 && statusDownload ) {  
    output.write(data, 0, count);
 }

 output.flush();
 output.close();
 input.close();

2- 下载完成时:

promptInstall = new Intent(Intent.ACTION_VIEW);             
File fileApk = new File (getFilesDir() + "/xx.apk");
promptInstall.setDataAndType(Uri.fromFile(fileApk) , Consts.APK_FILE_INSTALL_TYPE);
startActivityForResult(promptInstall,654);

3- 安装后(或如果取消),apk 文件被删除:

 File fileApk = new File (a.getFilesDir() + "/xx.apk" );
 fileApk.delete();

为了安装一个apk文件,它必须是“World Readable”,这意味着每个人都可以在下载文件之后(安装之前)获取apk文件。

有谁知道如何设置良好的权限以避免外部下载?

感谢您的阅读!

【问题讨论】:

    标签: android apk file-permissions android-install-apk


    【解决方案1】:

    有谁知道如何设置好的权限以避免外部下载?

    您已经拥有“良好权限”。安装程序在与您的不同进程中运行,因此它必须能够读取该文件。由于您无法为此仅向安装程序授予权限,因此您必须向所有可能的进程授予读取权限。

    对于大多数情况,我们会使用ContentProvider 来绕过这个限制。唉,at least as of a year ago, the installer did not support content:// Uri values

    【讨论】:

    • 感谢您的回答。对于 4.0 之前的 android 版本,内容提供程序不起作用...我会尝试寻找其他解决方案!
    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多