Environment.getExternalStorageDirectory() get hardware Storage - android

I'm trying to get all files list from SD card by Environment.getExternalStorageDirectory() but i get back list from hardware Storage. I tried hardcoded but result is that same. Any idea what could be causing this?
My code
String path = Environment.getExternalStorageDirectory().toString()+"/";
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "Size: "+ file.length);
for (int i=0; i < file.length; i++)
{
Log.d("Files", "FileName:" + file[i].getName());
}
Return
Path: /mnt/sdcard/
Size: 21
FileName:LOST.DIR
FileName:.android_secure
FileName:Music
FileName:Podcasts
But i dont`t have Podcasts etc. in SDcard.
Half answer: Environment.getExternalStorageDirectory() get inside memory storage in some case. If i try String path = "/mnt/external_sd/"; then its work. But in other devices storage is in other dir.

Try this code,
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()
+ "/Save PIP/");
dir.mkdirs();
File file = new File(dir, System.currentTimeMillis() + ".jpg");
// Utils.showAlert("Image Saved to SD Card", "PIP Effect", "Ok", FrameActivity.this);
if (file.exists()) file.delete();
try {
output = new FileOutputStream(file);
bitmap_final.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I wish that it will help you.

Related

I am unable to create a folder in external sd card in android

I have tried this code but it always creates a folder in the phone's internal memory.
String externalDataPath = Environment.getExternalStorageDirectory() + "/yourFolder";
File f = new File(externalDataPath);
try {
if (!f.exists()) {
f.mkdir();
}
externalDataPath = externalDataPath + "/" + filename;
f = new File(externalDataPath);
if (!f.exists())
f.createNewFile();
externalDataPath = Environment.getExternalStorageDirectory().toString();
} catch (IOException e) {
e.printStackTrace();
}
Also, I have added EXTERNAL_WRITE PERMISSION in Manifest file..
Any help would be highly appreciated.
A path to your app specific folder on a removable micro sd card can be obtained by using the second item returned by
getExternalFilesDirs()

How to see list of files stored in a directory

I have built an app that makes a folder in the storage of the device. Here is the code I'm using:
File wDirectory = new File("/sdcard/W/");
wDirectory.mkdirs();
File outputFile = new File(wDirectory, filename);
FileOutputStream fos = new FileOutputStream(outputFile);
Now I want to know how do I show files in a list view in this directory.
paste your folder path....
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}

Programmatically generated zip invalid on pc but not on android

SOLVED:
ok so I am basically stupid. I couldn't open the file because I forgot to install winrar or 7zip since this pc is newly formatted... Everything works fine. Sorry to waste anyone's time.
In my app I programmatically generate a .zip file from photos and .csv files in a directory.
It creates the zip and then sends the email with the attachment without a hickup. The problem however is that on my pc I can't extract the .zip file because it says it's invalid, but on my device using "WinZip" I can check my .zip file and it has everything it is suppose to have. This is confusing me.
Here is my code:
Here I check for which checkboxes have been checked then do the zipping
for(int i = 0; i < cbStates.size(); ++i)
{
if(cbStates.get(i))
{
String zipFile = Environment.getExternalStorageDirectory() + "/ArcFlash/" + listItems.get(i) + ".zip";//ex: /storage/sdcard0/ArcFlash/study12.zip
String srcDir = Environment.getExternalStorageDirectory() + "/ArcFlash/" + listItems.get(i);
try
{
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
File srcFile = new File(srcDir);
Log.i("customException", "going to compress");
addDirToArchive(zos, srcFile);
// close the ZipOutputStream
zos.close();
}
catch (IOException ioe)
{
System.out.println("Error creating zip file: " + ioe);
}
//Send the email
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"jbasson#powercoreeng.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");
String folderPath = Environment.getExternalStorageDirectory() + "/ArcFlash/" + listItems.get(i) + ".zip";
//Uri u = Uri.fromFile(folderPath);
//Log.i("customException", "uri path: " + u.getPath());
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + folderPath));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Toast.makeText(context,"Case \"" + studyName + "\" has been sent", Toast.LENGTH_LONG).show();
//adapter.setElement(i, adapter.getStudy(i) + "(sent)");
}
}
and here is the zip function:
private static void addDirToArchive(ZipOutputStream zos, File srcFile)
{
File[] files = srcFile.listFiles();
Log.i("customException", "Adding directory: " + srcFile.getName());
for (int i = 0; i < files.length; i++)
{
// if the file is directory, use recursion
if (files[i].isDirectory())
{
addDirToArchive(zos, files[i]);
continue;
}
try
{
System.out.println("tAdding file: " + files[i].getName());
// create byte buffer
byte[] buffer = new byte[2048];//1024
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getAbsolutePath() + "/" + files[i].getName()));//files[i].getName()
int length;
while ((length = fis.read(buffer)) > 0)
{
zos.write(buffer, 0, length);
}
zos.closeEntry();
// close the InputStream
fis.close();
}
catch (Exception ex)
{
Log.i("customException", "error zipping: " + ex.getMessage());
}
}
}
ok so I am basically stupid. I couldn't open the file because I forgot to install winrar or 7zip since this pc is newly formatted...

disallow images to display on gallery

I am have downloaded some images into my sd card and i would like to NOT display them in my gallery. Is there a way to remove them? I have read about .nomedia file but how to do I create them?
is it by doing this?
File storagePath = new File(Environment.getExternalStorageDirectory(),folderName+"/Covers/");
if (!storagePath.exists())
{
File file = new File(storagePath, ".nomedia");
if (!file.exists()) {
try {
file.createNewFile();
Log.d("created","successful");
}
catch(IOException e) {
}
}
}
Here's a snippet that will do what you want:
File storagePath = new File(Environment.getExternalStorageDirectory(),
folderName + "/Covers");
storagePath.mkdirs();
File filename;
filename = new File(storagePath + "/" + ".nomedia");
if (!filename.exists()) {
filename.createNewFile();
}
Note: It might throw an IOException if it cannot create the file, you should catch this.
It will take likely a reboot for the mediascanner to rescan and determine it should not index that location.

Writing a zip to internal storage -- file doesn't exist after download completion

I used a tutorial to download a zip into a subdirectory of my application's internal storage. I wrote the zip to /data/data/my.package.name/files/mySubDirectory/the.zip.
But, when I check to see whether the zip exists, it doesn't:
String fileDirectory = this.getFilesDir().getAbsolutePath() + "/mySubDirectory/the.zip";
File file = new File(fileDirectory);
if(file.exists()) {
Log.e(this.class.getName(), "file exists");
} else {
Log.e(this.class.getName(), "file doesn't exist");
}
I verified that fileDirectory is the same path as the File outFile for the FileOutputStream.
What could be the problem?
Try getting your file path as below :
String fileDirectory=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "data" + File.separator+ getActivity().getPackageName()+ File.separator +"mySubDirectory"+File.separator+"the.zip";
Using this SO question, I created a subdirectory using this example:
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
The problem is that I didn't expect the subdirectory to be prepended with "app_", so I was looking for the zip in the wrong place.
Try using getFilesDir() + "/" subdirectory + "/" "the.zip"
Without the getabsolutepath().
That is what I used could be the issue.
OK maybe you problem is with permissions do you see the file in the DDMS under data/data/package/files ? Check the permissions for the files
Here is my code
String path = getFilesDir() + "/"
+ subDirName + "/";
File file = new File(path);
file.mkdirs();
setReadable(file);
I use the following to make the file readable
#TargetApi(9)
private void setReadable(File file) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
try {
Runtime.getRuntime().exec(
"chmod 777 " + file.getCanonicalPath());
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
file.setReadable(true, false);
}
}
}

Categories

Resources