如何创建一个文件夹并将多个文件保存到android中的该文件夹?

时间:2012-06-07 09:12:39

标签: android android-sdcard

我使用如下所示的代码将按钮点击时将jpg文件保存到sdcard。

OutputStream fOut = null;
            try 
            {
                fOut = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
                mMergedLayersBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                fOut.flush();
                fOut.close();
            } 
            catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }

将图像保存到文件夹,我重写代码如下所示

OutputStream fOut = null;
            //Create Folder
              File folder = new File(Environment.getExternalStorageDirectory().toString()+"/draw/Images");
              folder.mkdirs();

              //Save the path as a string value
              String extStorageDirectory = folder.toString();

              //Create New file and name it draw.jpg
              File file = new File(extStorageDirectory, "draw.jpg");
              try 
                {
                    fOut = new FileOutputStream(file);
                    mMergedLayersBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                } 
                catch (FileNotFoundException e) 
                {
                    e.printStackTrace();
                } 
                catch (IOException e)
                {
                    e.printStackTrace();
                }

现在,当我第二次点击“保存”按钮时,它会重写图像。我想将保存的整个图像保存到文件夹中。我不知道如何更改我的代码以满足此要求。如果有人知道这件事,请帮助我..

1 个答案:

答案 0 :(得分:0)

由于您已将图像名称硬编码为"draw.jpg",因此每次覆盖图像时都会如此。因此,而不是硬编码提供动态名称。

例如,而不是

File file = new File(extStorageDirectory, "draw.jpg");

File file = new File(extStorageDirectory, System.currentTimeMillis()+"draw.jpg");
相关问题