Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Save images in an specific folder

Tags:

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" /> 
like image 407
Goblinch Avatar asked Aug 02 '13 07:08

Goblinch


People also ask

How do I save a picture to a folder on Android?

On your Android phone, open Gallery . Press and hold the photo or video that you want to move. Move to folder.

How do I save a picture in a folder?

Right-click the illustration that you want to save as a separate image file, and then click Save as Picture. In the Save as type list, select the file format that you want. In the File name box, type a new name for the picture, or just accept the suggested file name. Select the folder where you want to store the image.

How do I get a list of images in a specific folder on Android?

You can use below code to get all images from specific folder. 1) First you need to define File object to get the storage and appened the name of the folder you want to read. File folder = new File(Environment. getExternalStorageDirectory().

In which folder are the images stored in Android Studio?

In Android Studio inside the res folder, one can find the drawable folder, layout folder, mipmap folder, values folder, etc. Among them, the drawable folder contains the different types of images used for the development of the application.


2 Answers

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();     } } 
like image 97
mdDroid Avatar answered Oct 16 '22 08:10

mdDroid


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);     } } 
like image 25
stackflow Avatar answered Oct 16 '22 07:10

stackflow