0

我使用如下所示的代码在单击按钮时将 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();
                }

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

4

1 回答 1

0

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

例如,而不是,

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

尝试

File file = new File(extStorageDirectory, System.currentTimeMillis()+"draw.jpg");
于 2012-06-07T09:22:01.560 回答