【问题标题】:android: saving files to specific folder for later retrievalandroid:将文件保存到特定文件夹以供以后检索
【发布时间】:2013-01-16 17:39:30
【问题描述】:

我正在处理绘图部分,并编写了以下代码将图像保存到指定的相机文件夹。但是,我宁愿使用应用名称创建一个新文件夹并将图像保存到该文件夹​​中。那是怎么做到的?

我还想稍后从该特定文件夹中检索图像文件。

谢谢!

当前代码:

     String fileName = "ABC";

      // create a ContentValues and configure new image's data
      ContentValues values = new ContentValues();
      values.put(Images.Media.TITLE, fileName);
      values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
      values.put(Images.Media.MIME_TYPE, "image/jpg");

      // get a Uri for the location to save the file
      Uri uri = getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

      try 
      {                                               
         OutputStream outStream = getContext().getContentResolver().openOutputStream(uri);

         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

         outStream.flush(); // empty the buffer
         outStream.close(); // close the stream

【问题讨论】:

    标签: android outputstream


    【解决方案1】:

    试试这个可能对你有帮助。

    public void saveImageToExternalStorage(Bitmap image) {
        String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryName";
        try
        {
            File dir = new File(fullPath);
            if (!dir.exists()) {
            dir.mkdirs();
            }
            OutputStream fOut = null;
            File file = new File(fullPath, "image.png");
            if(file.exists())
                file.delete();
            file.createNewFile();
            fOut = new FileOutputStream(file);
            // 100 means no compression, the lower you go, the stronger the compression
            image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        }
        catch (Exception e)
        {
            Log.e("saveToExternalStorage()", e.getMessage());
        }
    }
    

    【讨论】:

      【解决方案2】:

      这个方法经过多次尝试才有效。

      private String getFilename() {
          String filepath = Environment.getExternalStorageDirectory().getPath();
          File file = new File(filepath,AUDIO_RECORDER_FOLDER);
      
          if(!file.exists()){
                  file.mkdirs();
          }
      
          return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-24
        • 2013-11-14
        • 2013-06-23
        • 2012-12-24
        • 2014-07-18
        • 2019-10-02
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多