i have a activity where i have a image view..i capture image and image get stored in sd card folder. at the time of capterd image a folder will create in sd card and the respective clicked image stored in sd card card folder. And absolute path of that image inserted in sqlite database. i get the table index of image and split the path to get file/image name..,,i want to know how to check that image name exist in sd card folder or not at the time of post image to server..
String trp=AppDBManager.w1; ///image name get in Aapdbmanager class variable w1
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(Environment.getExternalStorageDirectory()+ "/Retailsolution/"+trp+"");
if(dir.exists()){
//code
}
File root = new File(Environment.getExternalStorageDirectory(), mFolder);
if (root.exists()) {
File mFile;
mFile = new File(root, mFileNameOut);
if (mFile.exists()) {
//YOUR CODE
}
}
File file = new File(getExternalCacheDir(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
Try this:
File file = new File(path);
if(!file.exists()){
//your code if file not exists
}else {
}
Related
I wanted to create some folders and some files to /storage/MyFolder/.
I had tried to work on using /storage/emulated/0/MyFolder and it is working but i want to make hide under the storage root folder.
Is there any possible way for me to do this?
You can create folder like this in External Storage directory:
String folder_main = "NewFolder";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
//get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/Abc/";
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
if(!sdIconStorageDir.exists()){
sdIconStorageDir.mkdirs();
}
I would like to know if there is a way to store the image file of my application to the mobile external storage of the mobile phone but that the galery application does not display the images contained in this folder.
Add ".nomedia" file.
Save your files to a folder that starts with ".", e.g., /sdcard/.myimages/
You can create .nomedia file using this code:
String NOMEDIA=".nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
OR
Add storage permission in your manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private void hideMyFile(String original_name,String new_name)
{
try
{
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,original_name);//abc.png
File to = new File(sdcard,new_name);//abc
from.renameTo(to);
}
catch (Exception e)
{
e.printStackTrace();
}
}
So call this function hideMyFile() and pass two parameter and it will rename the file with no extension in this way those image will not appear in gallery
I'm using code in comment to save a file in directory folder. This code is working properly with mobile phone having sd card.
But if I use it with mobile phone not having sd card it is giving IO exception.
How can I create a folder like whatsapp has in device storage root to save images etc but I want a folder of application name to save .csv files in my device storage root.
First make sure you have added Storage Permissions inside Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you want to create folder inside ExternalDirectory
File f = new File(Environment.getExternalStorageDirectory()+"/foldername");
if(!f.exists()) f.mkdir();
If you want to create file inside internal storage
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory;
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
You can create directory in internal storage and external storage as Follows.
//check whether Sdcard present or not
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
String directory = "/your Directory name or app name ";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + directory;
File dir = new File(path);
if(!dir.exists()) //check if not created then create the firectory
dir.mkdirs();
//add your files in directory as follows
File file = new File(dir, filename);
}
else
{
// create folder in internal memory
File mydir = context.getDir(directory, Context.MODE_PRIVATE); //Creating an internal directry
if(!mydir.exists()) //check if not created then create the firectory
mydir.mkdirs();
//add your files in directory as follows
File file = new File(mydir, filename);
I hope it will work for you
I am trying to save a file in my External SDcard but the file is getting stored in internal storage. How to store the file in the SDcard inserted
mr = new MediaRecorder();
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
Log.e("aa","sss");
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(), "Recorder");
} else {
cameraFolder= getDir("Recorder",MODE_PRIVATE);
}
if(!cameraFolder.exists()) {
cameraFolder.mkdir();
}
fname = cameraFolder + "/myrec1.MPEG_4";
There is already a similar question here, you can also find more detailed information on the android docs
As suggested in a comment below I'll improve this answer.
In order to save a file to a specific position in your SD card you need to create a File object which points to that position:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/your/file/directory");
dir.mkdirs();
File file = new File(dir, "filename");
and then use a FileOutputStream to write the content.
Remember that you need to add the required permission in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
How can I create internal folder in my application to put the image captured in it
after that get the image folder path ???
inside Android you can create folder either on SD card (External Storage) or on internal memory.
the following command create a folder
file.mkdir()
so when you provide a file path like bellow
File file=new File(PATH/FOLDER_NAME);
it will create on specific location but if you do not provide path it will simply create folder on internal memory. which is basically
data/data/YOUR_APPLICATION_PACKAGE/
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
File file = new File(path, "/" + fname);
You can add whatever folder name you want after "DIRECTORY_MOVIES" or "DIRECTORY_PICTURES" or whatever
Simply you can add this code on your camera capture button
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Your Folder Name");
imagesFolder.mkdirs();
Calendar cal = Calendar.getInstance();
File photo = new File(Environment.getExternalStorageDirectory()
+ "/Your Folder Name", (cal.getTimeInMillis() + ".jpg"));
mCapturedImagePath = photo.getAbsolutePath();
Uri uriSavedImage = Uri.fromFile(photo);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, PICK_FROM_CAMERA);
The above code create a folder and save an Image into your SD card
path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES);
File file = new File(path, "/" + fname);
You can add whatever folder name you want after "DIRECTORY_MOVIES" or "DIRECTORY_PICTURES" or whatever - and it's internal to the device, and not external like on an SD card. It simply means that other apps can see this data and that it's not internal to the app and invisible to other apps or users