2012-06-07 168 views
0

使用下面显示的代码来保存JPG文件到SD卡上点击按钮。如何在android中创建文件夹并将多个文件保存到该文件夹​​?

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(); 
       } 

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

+2

更改此:' “draw.jpg”''到的String.format( “%D.JPG”,System.currentTimeMillis的()'在第二种方法中获得新的文件名。 –

+0

谢谢Harry Joy ....我明白了.... – Binu

回答

0

由于您已经将图片名称硬编码为"draw.jpg",因此每次您的图片被覆盖。因此,而不是硬编码提供一个动态名称。

对于实施例,代替,

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

尝试

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