Save file to internal storage in Android - java

I want to make a simple Android application which will make user write text and when he presses on save button I'll get the text from text area and create folder then save file into folder in the storage.
I did that but in external storage and here's my code
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfolder";
File f = new File(path);
f.mkdir();
PrintWriter pw = new PrintWriter(path + "/" + txt_name.getText() + ".txt");
pw.write(main_txt.getText().toString());
pw.close();
This code runs well but it runs only on devices which have an external memory card, so I want to do the same but save file on internal storage for device to make this code run on any device, how can I save file into internal storage?

No.
Your code saves to external memory which is build into the phone.
Your code has nothing to do with a removable micro SD card.

Related

How to create a new folder in android?

I want to create a new folder in external storage. I use this code:
val folderMain = "name"
val f = File(getExternalStorageDirectory(), folderMain)
if (!f.exists()) {
f.mkdirs()
}
After executing it works and creates a new folder in internal storage, not in external storage. How can I create a new folder in external storage?
I am 100% sure I have external storage in my device and it has 14 GB space available in it and location is /storage/extSdCard.
I have tested this code on two Samsung phones Android version jellybean and Kitkat but the same result.
You can have the answers to your questions here :
https://developer.android.com/training/data-storage
I also recommend you to check the result of mkdirs():
if (!f.exists() && !f.mkdirs()) {
Log.e("mDebug", "Couldn't create " + f.getName());
}
After executing it works and creates a new folder in internal storage, not in external storage.
No, it creates the file in what the Android SDK refers to as external storage.
I am 100% sure I have external storage in my device and it has 14 GB space available in it and location is /storage/extSdCard.
That represents removable storage.
How can I create a new folder in external storage?
Your existing code creates a directory in the root of external storage. You have no ability to write to arbitrary locations on removable storage.
The simplest places to write to on removable storage are in the locations supplied by the getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() methods on a Context. If those return 2+ locations, the second and subsequent ones are on removable storage, and you have full read/write access to them.
try this code:
String fileName = "myfile.zip";
File ZIP_Directory = new File("/sdcard/MOBstate/");
ZIP_Directory.mkdirs();
File outputFile = new File(PDF_Directory, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);

How to save a .GIF file into the gallery?

Why is this so difficult to do in android? I know its easy for images, but why not .gifs?
I have this code here which saves it to an SD card, but I am trying to account for the user not having an SD card.
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
File file = new File(dir, "GIFName_" + System.currentTimeMillis() +".gif");
try {
FileOutputStream f = new FileOutputStream(file);
f.write(generateGIF(list));
} catch(Exception e) {
e.printStackTrace();
}
My app basically converts images to .GIFS, and right now it saves it on the sd card, but I want to save it to the gallery. Is there any way to do this easily? I know you can do it for images, but can you for .GIFS that are created?
What exactly do you mean by Gallery? There is no directory called Gallery. However there is an application called Gallery and I hope that's what you mean.
Environment.getExternalStorageDirectory() will return the root path to the external storage. This has no dependency to the file you are trying to save. If you want save to the Pictures directory, then you can do Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
Gallery is an application in android that scans the whole system and adds all media items to it. If you try saving a file, be it .gif or .jpg, or .png, programmatically in Android, there is no guarantee that the file will be picked up by Gallery immediately. That's why you need to use MediaScannerConnection. This will let you add your newly created file to be shown in Gallery.
Something like below:
MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
Documentation: https://developer.android.com/reference/android/media/MediaScannerConnection.html#scanFile(java.lang.String, java.lang.String)

File existence in internal storage using java

I am creating an application were i am reading the content from a file present in internal storage and displaying in the application.
I am creating a file on the desktop and transferring that file to the mobile using usb cable.If sdcard is present the file will be stored in sdcard and for accessing the file i found the following code and it worked for me.
File path = Environment.getExternalStorageDirectory();
/storage/sdcard0/Filename.txt is the path which i am getting when i run the above code.If file is present in sdcard it is returning true else false.
If sdcard is not present and when i transfer the file from desktop to mobile it gets stored in internal storage.in this case how do i access my file.
In some cases i have come to know that sdcard0 is internal storage and not the external storage.
Which is the best way to find the file existence in internal storage of the mobile and not apps internal storage. please help.
Thanks in advance
To find out the path for your app's internal storage you can use the following commands:
Context context = getApplicationContext();
or
Context context = getApplication();
String dir = context.getFilesDir().getAbsolutePath();
String dir = context.getFilesDir().getPath();
getFilesDir() returns a File object that you can get the path from there. Then you can use that path to transfer your file. Also read below.
See here on how to write a file to internal storage and here on getting the path for internal storage.
The following code gives you the path for the your file in internal storage and then you can open it:
String yourFilePath = context.getFilesDir() + "/" + "filename";
File yourFile = new File( yourFilePath );
You can also use the following approach explained in here
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis = context.openFileInput(filename);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
fileContent.append(new String(buffer));
}
The above solution opens input stream to the file name provided using the context that gives the location where the files are stored in internal storage. In the while loop it reads from the input stream (up to size of buffer) every iterator and adds the read data to StringBuilder instance. The loop terminates when the length of the read data from input stream is -1.
If the file is not found, FileNotFoundException will be throw by openFileInput(filename) method.

How can i get internal storage and not sd card in android?

I know how to get the external storage we use:
String file = Enviroment.getExternalStorageDir();
Now I want to write my files on phone storage by using FileWriter and DataOutputStream.
Is it possible? How?
String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
This is used to get files from both external and interal storage in device

Make images and folder invisble to Gallery saved on SDcard?

Hi I want to make images invisible to android gallery or any third party gallery app, the image will be places in specific folder on SD card.
For example I have following code to save an image to a folder called myimages. I just want the images stored in myimages folder should not be visible to any gallery app and only my own application can access these images.
void saveBitmap(Bitmap bmp)
{
FileOutputStream os;
String dirName = "/mvc/mvc/myiamges/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
String root = Environment.getExternalStorageDirectory()
.toString();
File dir = new File (root + dirName);
boolean created=dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null),
// this.dirName+fileName);
//this function give null pointer exception so im
//using other one
File file = new File(dir, "aeg2.png");
os = new FileOutputStream(file);
}else{
os = openFileOutput("aeg2.png", MODE_PRIVATE);
}
bmp.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
}
Rename those files with custom extensions like filename.extension.customextension
like hello.avi.topsecret.
When you need the file to be ready mode to play rename it to proper extension, play and rename it back.
This should work for you.
or
Prefix your folder name with a dot "."
Check these links for more info:
http://www.makeuseof.com/tag/hide-private-picture-folders-gallery-android/
Yes, save it with any extension you want or even without extension.
In your app, just read it as normal image file.
Create an empty file inside your image store folder named '.nomedia' <- atention to the initial point.
All media files sabed inside this folder will not be showed in galery browsers.

Categories

Resources