Android SD card path inaccessible - android

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();
}
}

Related

Android App: Save a txt file that can be accessed through Explorer

I'm trying to create a txt file during the operation of my App which I then want to download onto my computer and assess the contents.
I've try both on the internal and external storage but I am still unable to find the text file after (when my tablet is plugged into my computer).
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("orp.txt",Context.MODE_WORLD_WRITEABLE));
outputStreamWriter.write(data);
outputStreamWriter.close();
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"orp2.txt");
String tempS = file.getAbsolutePath();
file.setReadable( true, false );
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
I have found been able to find either ("orp.txt" or "orp2.txt"), but also I do not receive any error during the running of the app, and the app is able to open "orp.txt" at a later time, so I know is has been created.
I cant find files created unless using Eclipse file explorer, or maybe its the media scan that has to be triggered, can be done by restarting the device. Scans the files available on the device.
It may be the directory, this is what I do in my code.
File f = new File(cxt.getExternalFilesDir(filepath), fileName);
f.createNewFile();
FileWriter writer = new FileWriter(f);
writer.append(manNo+System.getProperty("line.separator"));
writer.append(dateTime);
writer.flush();
writer.close();
Thanks for the input but I found the answer was to do with the media scanner, "orp2.txt" appeared in my download folder after I restarted the device.
This answer and blog post helped
Android: save file to downloads that can be viewed later

Unable to write to external SD Card in Android

I am trying to write files in the external SD card folder. Even after having set the required permission in the manifest file, I am unable to write on the external SD card.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Code:
String path = "/mnt/extsd/nit.txt";
File myFile = new File(path);
if (!myFile.exists()) {
try {
myFile.createNewFile();
} catch(Exception e)
{
txtText.setText("Failed-" + e.getMessage());
e.printStackTrace();
}
}
try {
FileOutputStream fostream = new FileOutputStream(myFile);
OutputStreamWriter oswriter = new OutputStreamWriter(fostream);
BufferedWriter bwriter = new BufferedWriter(oswriter);
bwriter.write("Hi welcome ");
bwriter.newLine();
bwriter.close();
oswriter.close();
fostream.close();
txtText.setText("success");
} catch(Exception e)
{
txtText.setText("Failed-" + e.getMessage());
e.printStackTrace();
}
On the other hand when I use ES File Explorer and try to create a file, it creates it without any issues.
Don't use the absolute path String path = "/mnt/extsd/nit.txt"; because you never know about android device being used by users. Rather you can get the external storage directory path by using Environment.getExternalStorageDirectory().toString().
You should be able to call Environment.getExternalStorageDirectory() to get the root path to the SD card and use that to create a FileOutputStream. From there, just use the standard java.io routines.
File log = new File(Environment.getExternalStorageDirectory(), "your_file_name.txt");
try {
out = new BufferedWriter(new FileWriter(log.getAbsolutePath(), false));
out.write("any data");
} catch (Exception e) {
}
And don't forget to close the streams.
First check sd-card is available or not.
String state = Environment.getExternalStorageState();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File folder = folder = new File(extStorageDirectory, "FolderName");
if(!folder.exists())
{
folder.mkdir();//making folder
}
File file = new File(folder,"Filename");//making file
}
Please try this code, it work in my application.

Saved Bitmap doesn't appear on SD Card

I am working on an app in which I would like to save some Bitmaps to the SD Card. I have looked at a lot of examples and other questions, and from that I have made the following code:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String dirPath = Environment.getExternalStorageDirectory().toString() + "/myFolder";
File dir = new File(dirPath);
dir.mkdirs();
String fileName = "bitmapname.jpg";
File file = new File(dirPath, fileName);
FileOutputStream fileOutPutStream;
try {
boolean created = file.createNewFile();
Log.d("Checks", "File created: " + created);
fileOutPutStream = new FileOutputStream(file);
fileOutPutStream.write(byteArrayOutputStream.toByteArray());
fileOutPutStream.close();
} catch (FileNotFoundException e) {
Log.d("Checks", "FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
Log.d("Checks", "IOException");
Log.d("Checks", e.getMessage());
e.printStackTrace();
}
I don't see what's wrong with this code. It doesn't give any errors and my app runs without crashing. However, when I connect my phone to my computer and open the SD Card I do not see the folder "myFolder" and I can not find the saved image anywhere. Do you guys have any ideas as to why this is?
EDIT: I noticed that I can see the saved bitmaps in the Android gallery, and they are indeed in a folder called "myFolder". However, I still don't see them when I connect my phone to my computer and browse my sd card.
From my experience I had similar issued when I forgot the fileOutPutStream.flush(); before the close().
Are you sure you are setting the permission to write to SD card? Try setting this one:
WRITE_EXTERNAL_STORAGE
Edit:
Ok, try this:
Environment.getExternalStorageDirectory().getAbsolutePath()
Instead of:
Environment.getExternalStorageDirectory().toString()
Or even create a directory like this:
File dir = new File(Environment.getExternalStorageDirectory() +
File.separator +
"myFolder");
dir.mkdirs();

Create a new file in Android emulator path /data/

I want to create a file in emulator android storage path /data/...
but it seems I can't create a new file by programs,
I should upload an empty file to /data/... and then write the file,
Can anyone help here?
openFileOutput("file.txt", MODE_PRIVATE) seems can create a new file in /data/data/Package/files/...
but it can't create in path /data/...
Thank you so much !
You cannot write directly to /data/ folder unless you have a rooted device. If you want to use /data/ directory then I would suggest using
/data/local/tmp/
This code can write a new file to the /data/data/Package/files/ folder. This may be the same with your code:
public void setparameter(int ilevel){
byte[] buffer = new byte[8];
buffer[0] = (byte)ilevel;
OutputStream output = null;
try{
output = openFileOutput("option.txt", MODE_WORLD_READABLE);
output.write(buffer);
output.flush();
output.close();
}catch (IOException e) { }
}

Storing data on SD Card in Android

Using the data-storage page in the docs, I've tried to store some data to the SD-Card.
This is my code:
// Path to write files to
String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/"+ctxt.getString(R.string.package_name)+"/files/";
String fname = "mytest.txt";
// Current state of the external media
String extState = Environment.getExternalStorageState();
// External media can be written onto
if (extState.equals(Environment.MEDIA_MOUNTED))
{
try {
// Make sure the path exists
boolean exists = (new File(path)).exists();
if (!exists){ new File(path).mkdirs(); }
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + fname);
fOut.write("Test".getBytes());
// Close output stream
fOut.flush();
fOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
When I create the new FileOutputStream I get a FileNotFound exception. I have also noticed that "mkdirs()" does not seem to create the directory.
Can anyone tell me what I'm doing wrong?
I'm testing on an AVD with a 2GB sd card and "hw.sdCard: yes", the File Explorer of DDMS in Eclipse tells me that the only directory on the sdcard is "LOST.DIR".
Have you given your application permission to write to the SD Card?
You do this by adding the following to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Before reading or writing to SD card, don't forget to check the SD card is mounted or not?
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

Categories

Resources