Android的 - 内部数据保存getFilesDir()? [英] Android - internal data save getFilesDir()?

查看:335
本文介绍了Android的 - 内部数据保存getFilesDir()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Web和APK更新文件下载与此code:

 网​​址URL =新的URL(网址[0]);
        HttpURLConnection的连接2 =(HttpURLConnection类)url.openConnection();
        connection2.setRequestMethod(GET);
        connection2.setDoOutput(真);
        connection2.connect();
        INT lenghtOfFile = connection2.getContentLength();        字符串PATH = ctx.getFilesDir()+/;
        文件apkdir =新的文件(路径);
        apkdir.mkdirs();
        文件newInstall =新的文件(apkdirapp.apk);        InputStream的输入=新的BufferedInputStream(url.openStream());
        OutputStream的输出=新的FileOutputStream(newInstall);        字节的数据[] =新的字节[1024];        总长= 0;        而((计数= input.read(数据))!= - 1){
            总+ =计数;
            publishProgress((int)的(总* 100 / lenghtOfFile));
            PROG =(int)的(总* 100 / lenghtOfFile);
            output.write(数据0,计);
        }        output.flush();
        output.close();
        input.close();
    }赶上(例外五){}    返回null;

和打开文件我用这个:

 意向安装=新意图(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(新文件(ctx.getFilesDir()+/ app.apk)),应用程序/ vnd.android.package归档);

和来这里的问题 - 我ablte要下载的应用程序存储到Context.getFilesDir(),但是当我及时安装pricess,logcat中说:无法读取AndroidManifest.xml中你在哪里看到这个问题,我需要?下载到内部,而不是入店内存的用户,因为安全的原因。


解决方案

  

我需要下载到用户的内部,而不是入店内存,因为安全的原因。


这完全是胡说八道。用户仍然可以得到你下载的文件,特别是如果你安装它。

您的问题是,安装程序无法读取该文件。无论是将其存储在外部存储,或者你需要使用 openFileOutput() MODE_WORLD_READABLE 而不是新的FileOutputStream 来得到你的的OutputStream

I am downloading from web and APK update file with this code:

        URL url = new URL(urls[0]);
        HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
        connection2.setRequestMethod("GET");
        connection2.setDoOutput(true);
        connection2.connect();
        int lenghtOfFile = connection2.getContentLength();

        String PATH = ctx.getFilesDir()+"/";
        File apkdir=new File (PATH);
        apkdir.mkdirs();
        File newInstall=new File(apkdir, "app.apk");

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(newInstall);

        byte data[] = new byte[1024];

        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int)(total*100/lenghtOfFile));
            prog=(int)(total*100/lenghtOfFile);
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {}

    return null;

and for opening file I use this:

    Intent install=new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(ctx.getFilesDir()+"/app.apk")), "application/vnd.android.package-archive");

And here comes the problem - I am ablte to store downloaded app into Context.getFilesDir(), but when I prompt installation pricess, logcat says "unable to read androidmanifest.xml. Where do you see the problem? I need to download into internal, not accesible memory by user, because of security reasons.

解决方案

I need to download into internal, not accesible memory by user, because of security reasons.

This is complete nonsense. A user will still be able to get at your downloaded file, particularly if you install it.

Your problem is that the installer cannot read the file. Either store it in external storage, or you will need to use openFileOutput() with MODE_WORLD_READABLE instead of new FileOutputStream to get your OutputStream.

这篇关于Android的 - 内部数据保存getFilesDir()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆