Default sdcard path in android - android

I'm having some problems with the path in my app.
A gallery of art asked me to make a video display that will show videos. So my idea was that they will only need to move the video file to sdcard default folder and rename the file to video.
But some cases (different tablets) the path does not match and the video will not open.
This is the code so far:
File sdcard = android.os.Environment.getExternalStorageDirectory();
File file = new File(sdcard, "video.mp4");
String src = file.getAbsolutePath();
video.setVideoPath(src);

Check to see if external media is useable with getExternalStorageState(). I think you'll need to see MEDIA_MOUNTED for your solution to work. This answer shows a snippet with safety check. Might be a more foolproof solution to search all mounted media for the specific file name. That way, user can copy the video anywhere and your app can play it.

Related

Android, how to choose save file location?

is there any solution how to choose the saving files location?
maybe with the original file browser, to choose the destination?
thank you!
All you need is Android Directory Picker
Better to save files with your app namespace:
String extStorage = Environment.getExternalStorageState();
path = extStorage+"/Android/data/com.mydomain.myapp/";
It will be deleted when app gets uninstalled.
Here is actually everything about data storing.
http://developer.android.com/guide/topics/data/data-storage.html
From the above link:
If you're using API Level 7 or lower, use
getExternalStorageDirectory(), to open a File representing the
root of the external storage. You should then write your data in the
following directory:
/Android/data/<package_name>/files/
It's probably easiest and expected to save this out to the dedicated area on the external SD card.
You can get this folder by calling
file://localhost/Applications/android-sdk-macosx/docs/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
You can also append a sub-folder to reference the source better ie. your app.
You can either choose DIRECTORY_PICTURES or DIRECTORY_MOVIES. If it's user created I'd probably stick to pictures.
To be helpful you can also call the media scanner to add it to the appropriate content provider system wide.
sendBroadcast( new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory()))
);
If you MUST get a file chooser going, which isn't the recomened approach to expose the user to the file system. Try the file chooser form Open Intents. http://www.openintents.org/en/node/164

google music/gallery picking up videos made by my app

This is the directory I'm saving media to:
Environment.getExternalStorageDirectory() + "/MyAppDir/";
The media (they're video files) are being picked up by google music and by the gallery. The strange thing is that it's only a few files that get picked up. How can I prevent this?
You can place a file name '.nomedia' in that folder. This will tell Android not to show it's content in the Gallery.
You can also use a folder name that begins with a dot '.' but this will hide it from users browsing their file system as well and may not be what you want.

mp3 that plays from SDcard won't play from my app's directory?

I am making an app that will download a zip archive from a location, unzip it and among files are some audio files(mp3) I need to play. The problem is mp3 files do not want to be played??
Now, I didn't C/P any code here since I don't think it might be a problem in the code. I have a Rom Toolbox installed on my phone and with that I copied an mp3 file off my SD card to my app's directory where other audio files are because I thought mp3 might be encoded incorrectly somehow. That is not the case since that same file, when I select it in RomToolbox and select to play it, it won't, but when I select that same file on SDcard it will play??
I don't think it is a matter of access rights since RomToolbox has superuser access.
So how can I get my mp3 files playing even if they are just copied/unzipped to a directory within my app's directory?
Thanks!
Ok, so I found the problem.
Apparently there is a problem with authorizations, but I am still puzzled why this file browser I have, that has superuser rights, can not play the file when in my directory.
Anyway, instead of setting the data source for media player from path I use FileDescriptor.
File tmpFile = new File(GlobalStore.appContext.getFilesDir() + File.separator + "audio" + File.separator + displayedPOI.getAudios().get(0));
FileInputStream tmpFIS = new FileInputStream(tmpFile);
mPlayer.setDataSource(tmpFIS.getFD());
This works now.

How to know where on the SD card the images are being stored, DCIM/Camera, DCIM/100MEDIA?

I have a method in my application which retrieves the last saved image in my DCIM/Camera folder and copies it to another location on the SD card. I've just tested it on another phone and found that it defaults saving to DCIM/100MEDIA. How am I able to get this path?
I ended up writing some code which looped through all the folders in the DCIM folder and retrieved the path of the lastModified() folder.
Looks like it is manufacturer dependent. In addition to using methods described in book, it seems also allowing the user to choose/override the default you "discover" would be an important option.
From Pro Android 3: p 579
Unfortunately, there is not a method call to tell you which directory might be used underneath the DCIM directory for Camera
pictures. There are a couple of methods though to tell you where the
top of the SD card is. The first is
Environment.getExternalStorageDirectory() and it returns a File object
for the top-level directory for the SD card.
See the following Google books link for full page text:
http://books.google.com/books?id=RuN0jb4YASwC&pg=PA579&lpg=PA579
other references:
Do all Android phones with a built-in camera use a folder called "DCIM" to store captured images?
http://androidforums.com/android-lounge/5930-definitive-androids-folder-structure.html#post239353
Maybe this can help you:
File picDir;
if (Build.VERSION.SDK_INT > 7)
picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
else
picDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Pictures/");
Hope this can help. Based on this.

Stop images on sd card from showing up in gallery?

I keep some png files on the user's sd card. Some users report that these images are showing up in their gallery application. Sounds like the gallery implementation on some devices search out any image files they can find on the sd card and show it in the gallery. Is there a way to tell the system not to include these images in the gallery? It's just an annoyance to the users.
Thanks
You can hide individual files from the gallery with a . prefix. For example .myimage.png
I'm not sure what version dependencies the above has and it doesn`t work with some third-party picture tools like the Gallery.
You should add a file named .nomedia into the directory where your images are. You may need to eject and re-insert the SD card before the images disappear from gallery (or otherwise trigger the media scanner) after creating this file on a phone that the Gallery has already picked up the files in the Gallery.
I think the .nomedia option is the best solution, although again, third party tools may not respect the .nomedia flag.
Hiding your files from the Media
Scanner Include an empty file named
.nomedia in your external files
directory (note the dot prefix in the
filename). This will prevent Android's
media scanner from reading your media
files and including them in apps like
Gallery or Music.
In http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
You may also consider saving those files as cache files or in the internal memory. More info in that link.
just keep the files in a folder and name the folder starting with a '.'
for example:
Pictures
Videos
.private--> will not be scanned for any media
Eboks
Along with these solutions using picasa is an alternative too... and .nomedia or .foldername suggestions are generally useful but...
Including accepted answer none of the solutions solve album covers problem.
A large number of android users are storing album covers inside the album folder and .dirname or .nomedia solutions are hiding all from both gallery and music player. The required functionality is hiding from gallery and showing in music player (else there is no reason for not deleting cover pics if we will not see them right? )
Here is the solution ;
Rename all album cover picture files to albumart.jpg
(or what picture you desire to hide from gallery)
This way gallery will not include them and music player will still use :)
Alternatively for any folder if you place a folder.jpg file in it that file will be ignored by gallery application.
As you can see we can only exclude max 2 files (pictures) from gallery application, not more :(
After you do this changes no need to clean caches of gallery and media scanner and reboot (still an option) just run an application that triggers scanning of media on every run. I prefer vaulty it refreshes media gallery on every run on photos, but there are lots of apps, maybe you already have one installed , just use it no need to install an application for just this media scan deal.
Knowledge source : http://code.google.com/p/android/issues/detail?id=35879
While making external directory use something like this
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory("YourDirectoryName"),"YourFolderName");
Instead of
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "YourFolderName");
These files won't be detected by gallery for more reference check saving media files on this link
http://developer.android.com/guide/topics/media/camera.html#intent-receive
Neither Windows nor Android allow the creation of the file .nomedia
It's seen as a file extension without a name.
i tried everything but only this solution worked 100% you need to plug in your urb and format the sdcard check quick format box and press on restore defaults and then it will show everything in ur gallery just you need to move all your images again :)
You can easily create the folder in Windows using Powershell
New-Item -path 'C:\Users\Google Drive\Pictures\.nomedia' -itemtype directory

Categories

Resources