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" />
On your Android phone, open Gallery . Press and hold the photo or video that you want to move. Move to 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.
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 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.
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(); } }
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With