【问题标题】:How to save file without SD Card?如何在没有 SD 卡的情况下保存文件?
【发布时间】:2013-01-27 19:27:05
【问题描述】:

我的设备没有存储卡,我想保存文件。我试过了:

filePath = Environment.getDataDirectory().toString();

/data/.... EACCES(权限被拒绝)

filePath = Environment.getDownloadCacheDirectory().toString();

/cache/.. EACCES(权限被拒绝)

filePath = Environment.getRootDirectory().toString();

/system/.. EROFS(只读文件系统)

还有其他方法吗?? (对不起我的英语:P)

【问题讨论】:

  • 将文件保存在内存中。在这里查看connectcoders.blogspot.in/2012/12/…
  • getFilesDir?并检查您的应用权限。
  • 文件将保存在哪里?我有“null/xxx.png”但我找不到这个
  • @Maury 你看不到内存中保存的文件。如果您想查看,请将其保存在外部存储器中。
  • 那么如何保存并查看文件呢? (当然没有卡:P)

标签: android file save


【解决方案1】:

尝试将文件写入内部存储,在您的应用程序本地空间中,文件将被写入类似的地方,下面的代码可能会在这方面对您有所帮助

            FileOutputStream fOut = null;
            OutputStreamWriter osw = null;
            try{
                fOut = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
                osw = new OutputStreamWriter(fOut);
                osw.write("yourString data");
                osw.close();
                fOut.close();
                }catch(Exception e){
                    e.printStackTrace(System.err); }

【讨论】:

    【解决方案2】:

    你可以试试这个。我正在使用它来存储下载图像

    String cacheDir= 
    context.getCacheDir();
    File file= new File(cacheDir,
    "test.dat");
    FileOutputStream out = new
    FileOutputStream(file);
    

    【讨论】:

      【解决方案3】:

      将文件保存在内存中,但您看不到保存在内存中的文件。

      使用此代码将文件保存在内存中。

      public boolean saveImageToInternalStorage(Bitmap image,Context context)
      {
          try {
              FileOutputStream fos = context.openFileOutput("photo.jpg", Context.MODE_WORLD_READABLE);
              image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                              // 100 means no compression, the lower you go, the stronger the compression
              fos.close();
              return true;
          }
          catch (Exception e) {
              Log.e("saveToInternalStorage()", e.getMessage());
          }
          return false;
      }
      

      阅读此article 以将文件保存在内部和外部存储器中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        • 2015-10-12
        相关资源
        最近更新 更多