Cannot view files written in sd card - android

I use the code below in order to save a file in my sdcard. The code is taken by another post in stackoverflow. The file is written in the card successfully and i can view it in my phone. However i noticed that i cannot view the file when i connect my phone in the computer. I also noticed that the contents of the folder ext_card are showing in my computer and not the contents of the folder sdcard.
Here is the code
File dir = new File ("/mnt/ext_card" + "/mine");
dir.mkdirs();
File file = new File(dir, "capture.csv");
try {
f = new FileOutputStream(file);
captureFile = new PrintWriter(f);
} catch (IOException ex) {
}
I write it inside the ext folder and i cannot view it again.

Related

Creating a directory on external sd card that will have a text file written to it

I am trying to create a folder on my external SDcard that will contain a text file that I can pull off my phone from my PC and read it.
I have pieced together a snippet that I believe should work, but I cannot find the folder that is supposedly being created.
File path = Environment.getExternalStorageDirectory();
File dir = new File(path.getAbsolutePath() + "/iptestdata/");
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = new SimpleDateFormat("yyyyMMddHHmmss'.txt'").format(new Date());
final File file = new File(dir, fileName);
As I am stepping though the debugger in Android Studio, I see the path being created and stored in dir is /storage/emulated/0/iptestdata, and I see the file name appearing in the format that I requested, but I cannot find the file via File Explorer in Windows. How can I fix this?
If you create File and store into storage Directory you want to call mkdirs()
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + " /dir - name /");
dir.mkdir();
File file = new File(dir, "File-Name.txt");
FileOutputStream os = outStream = new FileOutputStream(file);
String data = "This is the content of my file";
os.write(data.getBytes());
os.close();
Please check the Directory is created or not the following method
public static boolean canWriteOnExternalStorage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.v("Activity", "Yes, can write to external storage.");
return true;
}
return false;
}
Add Permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

write a file to a folder in the android device internal storage

I have a folder, called MyFolder, in the android device internal storage root directory. There is no external sd card mounted. The folder can be checked using says, ES file manager
I want to write a file to that directory.
I try the followings but seem all are not what I want. So how should sd be?
Please help.
File sd = Environment.getExternalStorageDirectory();
// File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) ;
// File sd = new File( Environment.getExternalStorageDirectory().getAbsolutePath());
// File sd = Environment.getRootDirectory() ; // system
// File sd = Environment.getDataDirectory() ;
backupDBPath = "MyFolder/_subfolder/mydata.txt";
File backupDB = new File(sd, backupDBPath);
If your directory located in the internal data directory of your app, you could write the code.
File directory = new File(this.getFilesDir()+File.separator+"MyFolder");
if(!directory.exists())
directory.mkdir();
File newFile = new File(directory, "myText.txt");
if(!newFile.exists()){
try {
newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fOut = new FileOutputStream(newFile);
OutputStreamWriter outputWriter=new OutputStreamWriter(fOut);
outputWriter.write("Test Document");
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
From the official document:
https://developer.android.com/guide/topics/data/data-storage.html#filesExternal
The device has removable(SD Card) or non-removable storage(Internal shared storage). Both are called external storage. Suppose you could create the directory in "Internal Shared storage", you could write the code below.
File directory = new File(Enviroment.getExternalStorage+File.separator+"MyFolder");//new File(this.getFilesDir()+File.separator+"MyFolder");
Note: If you have to use getExternalStorage, you should give the storage permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
From an app, you cannot write a file anywhere you'd like on the device's internal storage, it has to be located in the app's internal directory or app cache directory.
When saving a file to internal storage, you can acquire the
appropriate directory as a File by calling one of two methods:
getFilesDir() Returns a File representing an internal directory for
your app.
getCacheDir() Returns a File representing an internal
directory for your app's temporary cache files.
You could write:
String backupDBPath = "/_subfolder/";
String fileName = "mydata.txt";
File file = new File(context.getFilesDir() + backupDBPath, filename);
More infos here:
https://developer.android.com/training/data-storage/files.html

File upload picture android

I develop application with imageview, my problem is when I clicked button load picture, then will be shown gallery, then I want to copy selected image to res/drawable android, is it possible? if yes, can give me source code ?
Regards,
You cannot write to res/drawable. You can download image which can be stored in sdcard but you can't add images dynamically to drawable folder.
i am afraid res/ is read-only while running. Instead they give us the option or create files and our personal data in our application space
=====
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/app_name");
dir.mkdirs();
File file = new File(dir, "myAppData.jpg");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println(bytes);// bytes would be data from the source file
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
//..
} catch (IOException e) {
//...
}

Android create folders in Internal memory issue

I have a little issue with creating folders for my application in Internal Memory. I'm using this piece of code :
public static void createFoldersInInternalStorage(Context context){
try {
File usersFolder = context.getDir("users", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(usersFolder, "users.txt"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
File dataFolder = context.getDir("data", Context.MODE_PRIVATE);
File fileWithinMyDir2 = new File(dataFolder, "data.txt"); //Getting a file within the dir.
FileOutputStream out2 = new FileOutputStream(fileWithinMyDir2); //Use the stream as usual to write into the file.
File publicFolder = context.getDir("public", Context.MODE_PRIVATE);
File fileWithinMyDir3 = new File(publicFolder, "public.txt"); //Getting a file within the dir.
FileOutputStream out3 = new FileOutputStream(fileWithinMyDir3); //Use the stream as usual to write into the file.
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
So folders are created but in front of their name there is the beginning "app_" : app_users, app_data, app_public. Is there a way to create the folders with the name given by me? And another question : I want to first create folder Documents and than all other folders "Data, Public, Users" on it.... And the last question : How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?
Thanks in advance!
You can use this :
File myDir = context.getFilesDir();
String filename = "documents/users/userId/imagename.png";
File file = new File(myDir, filename);
file.createNewFile();
file.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(mediaCardBuffer);
fos.flush();
fos.close();
Is there a way to create the folders with the name given by me?
Use getFilesDir() and Java file I/O instead of getDir().
How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?
Use getFilesDir() and Java file I/O, such as the File constructor that takes a File and a String to assemble a path.

Android SD card path inaccessible

I have a knock off android device HT-PAD1051
But, really having trouble accessing the root directory in the built in sd card. card is partitioned and what is called "mnt/extsd" is accessible. Any ideas on how to get at root?
The device has been rooted.
Thanks
Joe
For any android device, If you want to get external storage always use.
Environment.getExternalStorageDirectory();
instead of giving hard coded path.
public void writeFile(String text){
try{
Writer output = null;
File dir = new File(Environment.getExternalStorageDirectory() + "/RequiredDirectory");
dir.mkdirs();
File file = new File(dir,<filename>);
output = new BufferedWriter(new FileWriter(file,true));
output.write(text);
output.close();
System.out.println("Your file has been written");
}catch (Exception e) {
e.printStackTrace();
}
}

Categories

Resources