I am working on an application where i need to create folder in the internal storage (phone memory) when the SD card is also available. I am using the Environment.getExternalStorageDirectory().toString()+"/"+folderName;
when i run this widout SD card it create folder in Internal but after inserting SD card when i run the same it create folder on SD card. But i want to create folder in Internal Storage.
Here is my Code
private static File contactsFile;
private static String contacts_storage_path;
contacts_storage_path = Environment.getExternalStorageDirectory().toString()+"/"+folderName;
contactsFile = new File(contacts_storage_path);
if (!contactsFile.exists())
{
//contactsFile.createNewFile();
contactsFile.mkdirs();
}
filecontact=new File(contactsFile, contact_file_name);
It is important to use Context.getDir(String value, int mode) to create directory in internal storage.
Refer below code -
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
Cheers :)
You can create a folder in Internal memory like this:
File dir = context.getDir("dir", Context.MODE_PRIVATE);
dir.mkdirs();
FileOutputStream outStream = null;
**File sdCard = Environment.getExternalStorageDirectory();**
**File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");**
dir.mkdirs();
//to know the path
Log.d(TAG, dir.getAbsolutePath();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.