无法建立的Andr​​oid里面的文件夹/数据 [英] Unable to create folder inside Android/data

查看:242
本文介绍了无法建立的Andr​​oid里面的文件夹/数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个问题,创造的Andr​​oid / data文件夹里面的文件夹中。
我的code需要创建文件夹,复制资产从一个文件夹里面。
这是我的code:

 私人文件getFileForShare(字符串文件名){
    尝试{
        。字符串extStorageDirectory = Environment.getExternalStorageDirectory()的toString();
        文件夹=新的文件(extStorageDirectory,/ Android的/数据/+ context.getPackageName());
        如果(!folder.exists()){
            folder.mkdir();
        }
        AssetManager上午= context.getAssets();
        为InputStream的InputStream = am.open(音频/+文件名);        文件f =新的文件(folder.getAbsolutePath(),文件名);
        的OutputStream的OutputStream =新的FileOutputStream(F);
        字节的缓冲区[] =新的字节[1024];
        INT长度= 0;        而((长度= inputStream.read(缓冲液))大于0){
            outputStream.write(缓冲液,0,长度);
        }        outputStream.close();
        inputStream.close();        返回F;
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    返回null;
}

我得到一个FileNotFoundException异常,当我初始化OutputStream对象。
另外,如果我去到Android / data文件夹,将创建与我的应用程序包名称的新文件夹。
清单里面我已经设置读取和写入存储权限。怎么了?


解决方案

  

怎么了?


正确的方式来获得一个目录,用于主设备所有者,驻留在的Andr​​oid /数据/ 外部存储,只是叫 getExternalFilesDir()上的可用上下文。替换:

 字符串extStorageDirectory = Environment.getExternalStorageDirectory()的toString()。
    文件夹=新的文件(extStorageDirectory,/ Android的/数据/+ context.getPackageName());

 文件夹= context.getExternalFilesDir();

I'm facing a problem with creation of folder inside Android/data folder. My code need to create folder, and copy a file from asset folder inside it. This is my code:

private File getFileForShare(String filename) {
    try {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extStorageDirectory, "/Android/data/" + context.getPackageName());
        if (!folder.exists()) {
            folder.mkdir();
        }
        AssetManager am = context.getAssets();
        InputStream inputStream = am.open("audio/" + filename);

        File f = new File(folder.getAbsolutePath(), filename);
        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) {
        e.printStackTrace();
    }
    return null;
}

I obtain a FileNotFoundException when i initialize OutputStream object. Also, if i go to Android/data folder, no new folder with my app package name is created. Inside manifest i've set read and write storage permission. What's wrong?

解决方案

What's wrong?

The proper way to get a directory that, for the primary device owner, resides under Android/data/ on external storage, is just to call getExternalFilesDir() on an available Context. Replace:

    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File folder = new File(extStorageDirectory, "/Android/data/" + context.getPackageName());

with:

    File folder = context.getExternalFilesDir();

这篇关于无法建立的Andr​​oid里面的文件夹/数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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