Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Save image taken from device memory to a specific folder in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 349
    Comment on it

    Step1: Permssion needed to save image.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    Step2: Saving image to the particular directory with new name as given by the user. This will make a seperate directory to the sd-card memory.

    String picName = "IMAGE_NAME" + Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + Calendar.getInstance().get(Calendar.MINUTE) + Calendar.getInstance().get	(Calendar.SECOND) + ".jpg";
    
      try {
    	String IMAGE_DIRECTORY_PATH = Environment.getExternalStorageDirectory() + File.separator + "DIRECTORY_NAME";
            File sd = new File(IMAGE_DIRECTORY_PATH);
            if (!sd.exists()) {
                 sd.mkdir();	// if directory dosen't exist creating new one
            }
    
            if (sd.canWrite()) {
                 Uri uri = data.getData();
                 String sourceImagePath = getPath(getActivity(), uri);
                 File source = new File(sourceImagePath);
                 File destination = new File(sd, profilePicName);
    
                 if (source.exists()) {
                      FileChannel src = new FileInputStream(source).getChannel();
                      FileChannel dst = new FileOutputStream(destination).getChannel();
                      dst.transferFrom(src, 0, src.size());
                      src.close();
                      dst.close();
    
                      imagePath = destination.getAbsolutePath();
                  }
            }
          } catch (Exception e) {
               e.printStackTrace();
          } 

    Step3: If the requirement is to delete files of that particular directory

    public static void delete(File f) throws IOException {
            if (f.isDirectory()) {
                for (File c : f.listFiles()) {
                    c.delete();
                }
            } else if (f.getAbsolutePath().endsWith("FIR")) {
                if (!f.delete()) {
                    new FileNotFoundException("Failed to delete file: " + f);
                }
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: