Search code examples
androidandroid-file

How to save image in specific sd card folder with using current time and data format in android?


I want to save a captured image in an SD-card specific folder with current time mills name of the image.

How can I do this?


Solution

  • try this after getting the bitmap object

     private void save_Image_to_sdcard() {
        // TODO Auto-generated method stub
        OutputStream file_outputstream = null;
        InputStream in = null;
        try {
            URL path = new URL(url);
            in = path.openStream();
    
    
    
            String path1 =    
    Environment.getExternalStorageDirectory().toString();
            System.out.println(path1+ "    " +user_file_name);
            // /mnt/sdcard
            File f = new File(path1 + "/"+System.currentTimeMillis()+".png");
            f.createNewFile();
            System.out.println("file created " + f.toString());
    
            file_outputstream = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = in.read(buffer, 0, buffer.length)) >= 0) {
    
                file_outputstream.write(buffer, 0, bytesRead);
    
            }
    
            file_outputstream.close();
            in.close();
    
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.v("hey", "error");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.v("hey", " ioexception");
        }
    
    }