Android - Save images in an specific folder

Related searches

I need to save the pictures taken with my app in an specific folder. I've read many solutions to this problem but I couldn't make any of them work so I ask for help.

MainActivity.java

public void onClick(View v) {

    Intent camera = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    //Folder is already created
    String dirName = Environment.getExternalStorageDirectory().getPath()
            + "/MyAppFolder/MyApp" + n + ".png";

    Uri uriSavedImage = Uri.fromFile(new File(dirName));
    camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(camera, 1);

    n++;
}

AndroidManifest.xml

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

Go through the following code , its working fine for me.

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/DirName/");
        wallpaperDirectory.mkdirs();
    }

    File file = new File("/sdcard/DirName/", fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Android - Save images in an specific folder, import android.os.Environment; void saveImage(){ try. { String filename = "monimageProcessing.jpg" ; save (directory + "/" + filename); println ( "File saved successfully." ); catch (Exception e) { println ( "Error while saving file: " + e.getMessage()); } } void mousePressed (){ saveImage(); Save image in specific folder in android device. Ask Question .But i need save it in a specific folder in external memory.This is my code.How i can do it?

I have used mdDroid's code like this:

public void startCamera() {
    // Create photo
    newPhoto = new Photo();
    newPhoto.setName(App.getPhotoName());

    //Create folder !exist
    String folderPath = Environment.getExternalStorageDirectory() + "/PestControl";
    File folder = new File(folderPath);
    if (!folder.exists()) {
        File wallpaperDirectory = new File(folderPath);
        wallpaperDirectory.mkdirs();
    }
    //create a new file
    newFile = new File(folderPath, newPhoto.getName());

    if (newFile != null) {
        // save image here
        Uri relativePath = Uri.fromFile(newFile);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath);
        startActivityForResult(intent, CAMERA_REQUEST);
    }
}

save images in specific folders(Android), Save Image Tutorial : In This Video, You Will Learn How to Save Image to Specific Duration: 7:03 Posted: Mar 10, 2019 android: save images to specific folder in the sd card. Ask Question Asked 7 years, 11 months ago. Active 2 years, 4 months ago. Viewed 11k times 1. 1. i have here a

Use Like this. It will work for you.

public void onClick(View v) {
  Intent camera = new Intent(
  android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(camera, 1);
}

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
  super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

  switch(requestCode) {
    case 1:
      if(resultCode == RESULT_OK) {
      Uri selectedImage = imageReturnedIntent.getData();
      String[] filePathColumn = {MediaStore.Images.Media.DATA};

      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      //file path of captured image
      filePath = cursor.getString(columnIndex);
      //file path of captured image
      File f = new File(filePath);
      filename= f.getName();

      Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show();
      Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show();
      cursor.close();

      //Convert file path into bitmap image using below line.
      // yourSelectedImage = BitmapFactory.decodeFile(filePath);
      Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show();

      //put bitmapimage in your imageview
      //yourimgView.setImageBitmap(yourSelectedImage);  

      Savefile(filename,filePath);
    }
  }
}

public void Savefile(String name, String path) {
  File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/");
  File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png");

  if(!direct.exists()) {
    direct.mkdir();
  }

  if (!file.exists()) {
    try {
      file.createNewFile();
      FileChannel src = new FileInputStream(path).getChannel();
      FileChannel dst = new FileOutputStream(file).getChannel();
      dst.transferFrom(src, 0, src.size());
      src.close();
      dst.close();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

Hope this will help you. for reference to use camera intent.

How to Save Image in External Storage in Android Studio, You can save captured image to a specific folder by this code: Capturing images from camera and setting into listview in android[^]. I made a program for downloading images, the problem is I can not save them in an orderly way. when I have an image is no problem with the order, but my idea is to deal with over 300 different images and I want a quick program. I want to create folders with different categories. However although I get to do what I want in java, I fail in android.

I have found an easier code to do it.

This is the code for creating the image folder:

private File createImageFile(){
        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/App Folder/";

        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "AppName_" + timeStamp;

        String file = dir +imageFileName+ ".jpg" ;
        File imageFile = new File(file);

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = imageFile.getAbsolutePath();

        return imageFile;
    }

,And this is the code for launching the camera app and take the photo:

public void lunchCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = createImageFile();
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.ziad.sayit",
                        photoFile);

                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

Useful link for different ways of doing it: https://www.programcreek.com/java-api-examples/?class=android.os.Environment&method=getExternalStoragePublicDirectory

How to save clicked image in a custom folder of the app , So you should instead use shared storage to save those types of files to the appropriate media collection. Note: To further protect app-specific files, use the Security library that's part of Android Jetpack to encrypt these files at rest. The encryption key is specific to your app.

Photos taken on Camera (the standard Android app) are stored on either a memory card or in phone memory depending on the settings of the phone. The location of photos is always the same - it’s the DCIM/Camera folder. The full path looks like this: /storage/emmc/DCIM - if the images are on the phone memory.

Android uses a file system that's similar to disk-based file systems on other platforms. The system provides several options for you to save your app data: App-specific storage: Store files that are meant for your app's use only, either in dedicated directories within an internal storage volume or different dedicated directories within external

Let’s store pictures in public pictures directory which can be obtained by calling getExternalStoragePublicDirectory () passing DIRECTORY_PICTURES to it. To access public directory, we need to request for READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions.

Comments
  • Check the answers to this question here: stackoverflow.com/questions/12995185/…
  • Have you checked if the folder is really created?
  • Check out the link.. stackoverflow.com/questions/7887078/…
  • I've already checked both and none of them worked for me
  • Possible duplicate of How to save images from Camera in Android to specific folder?
  • How can I get a Bitmap object with my picture?
  • take image from camera and write the Bitmap photo = (Bitmap) data.getExtras().get("data"); in on activity result.
  • I just discovered that it was a problem with my OS (Ubuntu). It didn't show me the pictures but they were in the correct folder
  • Hi. I am getting exception: java.io.FileNotFoundException: /Znapo/5/1838: open failed: ENOENT (No such file or directory)
  • using compress, spoils the image and making it blur, how to get original quality
  • Perfect, why is this not at the top
  • Great! after a lot of "solutions" with modifying the filename after it is captured, this is a short and nice solution.