android: saving files to specific folder for later retrieval
Asked Answered
C

2

6

I am working on a drawing part, and have written the following code to save the image to the designated camera folder. However, I would rather like to create a new folder using the app name and save the image to the folder. How could that be made?

I would also like to later on retrieve the image files from that specific folder too.

Thanks!

Current code:

     String fileName = "ABC";

      // create a ContentValues and configure new image's data
      ContentValues values = new ContentValues();
      values.put(Images.Media.TITLE, fileName);
      values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
      values.put(Images.Media.MIME_TYPE, "image/jpg");

      // get a Uri for the location to save the file
      Uri uri = getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

      try 
      {                                               
         OutputStream outStream = getContext().getContentResolver().openOutputStream(uri);

         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

         outStream.flush(); // empty the buffer
         outStream.close(); // close the stream
Caylor answered 16/1, 2013 at 17:39 Comment(0)
P
12

Try this it might help you.

public void saveImageToExternalStorage(Bitmap image) {
    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryName";
    try
    {
        File dir = new File(fullPath);
        if (!dir.exists()) {
        dir.mkdirs();
        }
        OutputStream fOut = null;
        File file = new File(fullPath, "image.png");
        if(file.exists())
            file.delete();
        file.createNewFile();
        fOut = new FileOutputStream(file);
        // 100 means no compression, the lower you go, the stronger the compression
        image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
    catch (Exception e)
    {
        Log.e("saveToExternalStorage()", e.getMessage());
    }
}
Purchasable answered 16/1, 2013 at 17:48 Comment(0)
H
-1

This Method works after so many try.

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);

    if(!file.exists()){
            file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
Haemic answered 10/3, 2014 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.