FileOutputStream illegalArgumentException“/”不允许

时间:2015-03-05 22:16:58

标签: android database fileoutputstream illegalargumentexception internal-storage

我花了好几个小时试图解决这个问题。 我正在尝试编写一个下载到我的内部android文件夹的图像。 唯一的问题是我需要将不同的图像存储在应用程序数据文件夹的子目录中,因为它们连接到与文件夹名称具有相同ID的对象。 我从文档中了解到,FileOutputStream不允许输出到子目录,只能输出到“root”应用程序数据文件夹。 这有解决方法吗? 这是我的代码:

 public static void writeImageToInternal(byte[] inImg, String FolderName, Context ctx) {

    File file = new File(ctx.getFilesDir().getAbsolutePath() + "/img/" + FolderName);
    if (!file.exists())
        file.mkdirs();

    int Name= 0;
    // TODO: Find Available Name
    File mypath = new File(file, FolderName + Name + ".png");
    FileOutputStream fOut = null;
    try {
        // mypath.createNewFile();
        fOut = new FileOutputStream(mypath);
        BitmapFactory.decodeByteArray(inImg, 0, inImg.length).compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

如上所述,原因是当从数据库加载对象时,应用程序搜索名为object id的文件夹,并返回该文件夹中的所有图像。 有没有办法让这个工作?

其次, 因为我将不同的图像连接到特定的对象,所以最好将我的图像作为BLOBS保存在数据库中吗? 我的应用程序上已经有一个数据库,我不能使用相同的数据库,因为数据库在服务器上备份,我不想将图像复制到。 它们涉及许多图像文件,因此如果数据库实现更好,是否可以将数据库保存到外部存储? 提前谢谢!

0 个答案:

没有答案