Android device specific camera path issue - android

I know that my question is a duplicate of this question and this one. But still I have some problems, so don't try to close or downvote my question friends. Please go through the question fully.
In my app I want to store Images into the same folder as where the device native Camera stores them. As per my research, I have found out that each device stores the Images into a different name and in different folder.
That is:
Nexus-One stores its camera files into a folder named Camera (/sdcard/DCIM/Camera).
All HTC devices store their camera files into a folder named 100MEDIA (/sdcard/DCIM/100MEDIA).
Sony Xperia x10 stores its camera files into a folder named 100ANDRO (/sdcard/DCIM/100ANDRO).
Motorola MilesStone stores its camera files into a folder named Camera (/sdcard/DCIM/Camera).
So I wanted to know if it's programmatically possible to get this path so that I can store the images taken from my app to the same location?
When I was googling I found out that it's better to create an external folder of my own so that I can store the images in that path with the name I am specifying. After that also in HTC device of API version 3 (Android 1.5) I found out that only the folder is getting created but the image gets saved in its default place with a name of its own.
How to solve this issue? Is it not possible to find the specific path and name of the image that gets saved for each device? Please help me friends.

Use getExternalStorageDirectory() if API level is below 7 and then append /Pictures to get the path of Photos storage.
for API level > 7 use getExternalStoragePublicDirectory (DIRECTORY_PICTURES).

I use the following code
String pictures_path = Utils.getRealPathFromURI(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
File path = new File(pictures_path);
if (path.isFile())
path = path.getParentFile();
Where Utils:
public static String getRealPathFromURI(Uri content_uri, int media_type) {
String column = MediaType.MEDIA_TYPE_PICTURE;
ContentResolver content_resolver = getContext().getContentResolver();
String [] proj = { column };
Cursor cursor = content_resolver.query(content_uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(column);
if (!cursor.moveToFirst())
return null;
return cursor.getString(column_index);
}
EDIT: Unfortunatelly, the approach above may not always work :( ...
Eventually I did manual checkings:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if (path.exists()) {
File test1 = new File(path, "100MEDIA/");
if (test1.exists()) {
path = test1;
} else {
File test2 = new File(path, "100ANDRO/");
if (test2.exists()) {
path = test2;
} else {
File test3 = new File(path, "Camera/");
if (!test3.exists()) {
test3.mkdirs();
}
path = test3;
}
}
} else {
path = new File(path, "Camera/");
path.mkdirs();
}

This is the function that I am using. It doesnt specifically name 100Media or Camera directories. It simply starts in the /DCIM folder and looks at its children. If the child is not a .thumbnails directory (or is, depending on what youre doing), then look at its children and get them. It also gets pictures in the /DCIM directory itself.
File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File thumbnails = new File(dcim, "/.thumbnails");
File[] listOfImg = dcim.listFiles();
if (dcim.isDirectory()){
//for each child in DCIM directory
for (int i = 0; i < listOfImg.length; ++i){
//no thumbnails
if( !listOfImg[i].getAbsolutePath().equals(thumbnails.getAbsolutePath()) ){
//only get the directory (100MEDIA, Camera, 100ANDRO, and others)
if(listOfImg[i].isDirectory()) {
//is a parent directory, get children
File[] temp = listOfImg[i].listFiles();
for(int j = 0; j < temp.length; ++j) {
f.add(temp[j].getAbsolutePath());
}
}else if(listOfImg[i].isFile()){
//is not a parent directory, get files
f.add(listOfImg[i].getAbsolutePath());
}
}
}
}
I have been looking for a solution to this problem myself. Since different manufacturers have differnet naming conventions for camera folders, this seems to cover all ground best. It works for me pretty well.

Related

Android Media Store API does not find image which have been created by Google Photo in External Storage

I try to implement a gallery app which also shows all locally stored images files grouped by their containing directories.
To this end I build upon the MediaStore API.
My code does not find local image directories, if these directories and the image files were created by Google Photos.
But if I use Google Files, to copy/move/create an image file in the affected directory, then my app finds that image.
What am I doing wrong?
Note: I am talking about real filesystem directories below the Pictures/ directory on the externals storage. I am not talking about albums which Google Photos creates in the cloud.
According to the developer documentation the MediaStore API should find all images below the Pictures/ directory.
Images, including photographs and screenshots, which are stored in the DCIM/ and Pictures/ directories. The system adds these files to the MediaStore.Images table.
Simplified code:
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Simplified data class
public class LocalFolder {
public final long id;
public final Uri coverUri;
public LocalFolder( final long id, final Uri coverUri ) {
this.id = id;
this.coverUri = coverUri;
}
}
Simplified code to seed the internal database
protected TreeMap< Long, LocalFolder > database;
public void run() {
final Uri collection = ( Build.VERSION.SDK_INT < Build.VERSION_CODES.Q ) ?
MediaStore.Images.Media.EXTERNAL_CONTENT_URI :
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
final String[] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_ID };
final Cursor cursor = contentResolver.query(
collection, projection, null, null, null
);
if(cursor == null) return;
// Cache column indices
final int imageIdColumn = cursor.getColumnIndexOrThrow( MediaStore.Images.Media._ID );
final int folderIdColumn = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.BUCKET_ID );
while( cursor.moveToNext() ) {
// Compile all required attributes
final long imageId = cursor.getLong( imageIdColumn );
final long folderId = cursor.getLong( folderIdColumn );
final Uri coverUri = ContentUris.withAppendedId( collection, imageId );
// If the image is contained in a folder, which already has been processed, skip
if( database.containsKey( folderId ) )
continue;
// Create the folder and take the current image as cover image
final LocalFolder localFolder = new LocalFolder( folderId, coverUri );
database.put( folderId, localFolder );
}
cursor.close();
}
Steps to reproduce:
Create a new, shared image file
Open your favorite web browser and download an image file
The image file is put into the shared Downloads/ directory
Above code finds the image (good!)
Move image file into a new real, file-system based directory with Google Photos
Open Google Photos
Open the register "Library"
Open the album "Downloads"
Open the recently added image
Tap "Settings" (thee dots in the upper right corner)
Tap "Move to folder"
Tap "New folder"
Create a new folder
Above code does not find the image (bad!)
"Re-create" the image file using Google Files
Open Google Files
Tap hamburger menu
Tap device
Open the directory Pictures/
Open the sub-directoy which corresponds to the newly created directory by Google Photos
Long-tap the recently added image file
Move the image file and move the image file back into the folder
Above code finds the new folder and the image (good!)

Android, Xamarin: Get File Path Of SD Card

I am currently working on an app, that goes through your phone and lists all available MP3 files. I managed to get this done and search for everything on the internal storage, but didnt manage to find a way using the envoirment to get to the sd card, when one is installed. This is my code - u will see a missing part when SD card is TRUE. Can you complete it?
public List<string> ReturnPlayableMp3(bool sdCard)
{
List<string> res = new List<string>();
string phyle;
if(sdCard)
{
// missing
}
else
{
try
{
var path1 = Android.OS.Environment.ExternalStorageDirectory.ToString();
var mp3Files = Directory.EnumerateFiles(path1, "*.mp3", SearchOption.AllDirectories);
foreach (string currentFile in mp3Files)
{
phyle = currentFile;
res.Add(phyle);
}
}
catch (Exception e9)
{
Toast.MakeText(ApplicationContext, "ut oh\n" + e9.Message, ToastLength.Long).Show();
}
}
return res;
}
}
It would need to return the exact same thing as it does for the internal storage only this time for the sd card. Right now, what is beeing returned is:
""/storage/emulated/0""
I hope you can help me. Thank you!
SO I found the place it is: /storage/05B6-2226/
But the digits refer to only MY sd card. How do I get this path programatically?
Take a look at these methods:
Context.GetExternalFilesDir
Returns the absolute path to the directory on the primary external
filesystem (that is somewhere on Environment.ExternalStorageDirectory)
where the application can place persistent files it owns. These files
are internal to the applications, and not typically visible to the
user as media.
Context.GetExternalFilesDirs
Returns absolute paths to application-specific directories on all
external storage devices where the application can place persistent
files it owns. These files are internal to the application, and not
typically visible to the user as media.
I've been searching for a couple of days with a lot of solutions that just ended up giving you the 'external' built in storage. Finally found this solution for the 'removable' SD Card and wanted to post it here in case someone else is still looking.
How to write on external storage sd card in mashmallow in xamarin.android
//Get the list of External Storage Volumes (E.g. SD Card)
Context context = Android.App.Application.Context;
var storageManager = (Android.OS.Storage.StorageManager)context.GetSystemService(Context.StorageService);
var volumeList = (Java.Lang.Object[])storageManager.Class.GetDeclaredMethod("getVolumeList").Invoke(storageManager);
List<Java.IO.File> ExtFolders = new List<Java.IO.File>();
//Select the Directories that are not Emulated
foreach (var storage in volumeList)
{
Java.IO.File info = (Java.IO.File)storage.Class.GetDeclaredMethod("getDirectory").Invoke(storage);
if ((bool)storage.Class.GetDeclaredMethod("isEmulated").Invoke(storage) == false && info.TotalSpace > 0)
{
//Get Directory Path
Console.WriteLine(info.Path);
}
}
Just wanna share my answer, where I have get the extStorages Path and I use this method in my simple file browser app.
public static string[] GetRemovableStorages()
{
List<string> extStorage = new List<string>();
//If this throws exception
string storageDir = (string)Environment.StorageDirectory;
//Try this
string storageDir = Directory.GetParent (Environment.ExternalStoragePublicDirectory).Parent.FullName;
string[] directories = Directory.GetDirectories(storageDir);
foreach(string dir in directories)
{
try
{
var extStoragePath = new Java.IO.File(dir);
bool isRemovable = Environment.InvokeIsExternalStorageRemovable(extStoragePath);
if(isRemovable) extStorage.Add(extStoragePath.AbsolutePath);
else return null;
}
catch
{
}
}
return extStorage.ToArray();
}
Elikill58's answer throws exception no such method "getDirectory" in my case but I recommend Elikill58's answer

How to efficiently create subfolders using Storage Access Framework?

I'm currently using the below code to create subfolder in MicroSD on Lollipop using SAF
String[] folders = fullFolderName.replaceFirst(UriFolder + "/", "").split("/");
//fullFolderName is a String which represents full path folder to be created
//Here fullFolderName = /storage/MicroSD/MyPictures/Wallpapers
///storage/MicroSD/MyPictures/ already exists
//Wallpapers is the folder to be created
//UriFolder is String and contains /storage/MicroSD
//folders[] will have folders[0]="MyPictures" folders[1]="Wallpapers"
DocumentFile Directory = DocumentFile.fromTreeUri(context, Uri.parse(treeUri));
//treeUri is the uri pointing to /storage/MicroSD
//treeUri is a Uri converted to String and Stored so it needs to parsed back to Uri
DocumentFile tempDirectory = Directory;
//below loop will iterate and find the MyPictures or the parent
//directory under which new folder needs to be created
for(int i=0; i < folders.length-1; i++)
{
for(DocumentFile dir : Directory.listFiles())
{
if(dir.getName() != null && dir.isDirectory())
{
if (dir.getName().equals(folders[i]))
{
tempDirectory = dir;
break;
}
}
}
Directory = tempDirectory;
}
Directory.createDirectory(folders[folders.length-1]);
The above code works fine and creates subdirectory but it takes ~5 Secs to create the folder. I'm new to SAF so is this the only way to locate subdirectories or is there any other efficient way to create subdirectories?
On internal storage I will use
new File(fullFolderName).mkdir();
Which will create folder in a fraction of second.
Here is a bit efficient way to create
public static boolean createFolderUsingUri(String fullFolderName,String treeUri,
String UriFolder,Context ctx)
{
String[] folders = fullFolderName.replaceFirst(UriFolder + "/", "").split("/");
//fullFolderName is a String which represents full path folder to be created
//Example: fullFolderName = /storage/MicroSD/MyPictures/Wallpapers
//The path /storage/MicroSD/MyPictures/ already exists
//Wallpapers is the folder to be created
//UriFolder is String and contains string like /storage/MicroSD
//folders[] will have folders[0]="MyPictures" folders[1]="Wallpapers"
//treeUri string representation of Uri /storage/MicroSD
//Ex: treeUri content://uritotheMicroSdorSomepath.A33%0A
DocumentFile Directory = DocumentFile.fromTreeUri(ctx, Uri.parse(treeUri));
for(int i=0; i < folders.length-1; i++)
{
Directory=Directory.findFile(folders[i]);
}
Directory.createDirectory(folders[folders.length-1]);
return true;
}
The method described in question took ~5 Secs, whereas this method takes ~ 3 Secs. On CM file manage the folder creation on same path took ~4 Secs so this is comparatively faster method. Yet searching more faster way which will take < 1 Sec

Get path of DCIM folder on both primary and secondary storage

I am writing an application that should upload pictures taken by the camera which are stored in the DCIM/Camera folder on both internal memory and SD card.
This means that before every upload all available storages have to be checked for presence of any images.
I use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) to access the primary storage, which can be either SD card or internal memory (depends on device).
From documentation:
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
My question is, how can I access the secondary storage to check for presence of images in the DCIM/Camera folder without hard-coding the path, which does not work well since the SD card might be emulated in different path.
Try this code for choosing the last used DCIM/Camera folder.
String getDCIMCamera() {
try {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE};
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor != null) {
cursor.moveToFirst();
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
if (path.contains("/DCIM/")) {
File file = new File(path);
path = file.getParent();
cursor.close();
return path;
}
} while (cursor.moveToNext());
cursor.close();
}
}
catch (Exception e) {
}
return "";
}
See also #Pedram tips: https://stackoverflow.com/a/24189813/966789
Try
File[] aDirArray = ContextCompat.getExternalFilesDirs(context, null);
See http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getExternalFilesDirs(android.content.Context, java.lang.String)
Remember that external storage (SD Card) is not emulated and all paths are visible to all users.
Return Value
null - In case of failure
Root path (File) of each mounted external storage.
So if aDirArray.length > 1, then the following gets you the DCIM path you are looking for on the removable storage.
File aExtDcimDir = new File(aDirArray[1], Environment.DIRECTORY_DCIM);
Maybe in your case, you want to check aDirArray[0], aDirArray[1], ... (all Files returned in the array) for the presence of Environment.DIRECTORY_DCIM.
getExternalStorageDirectory retrieves only from internal storage data but not secondary storage images
This is the way we are getting secondary storage images from android or any folder accessing from secondary storage this is the code......
String secStore = System.getenv("SECONDARY_STORAGE");
File file = new File(secStore + "/DCIM");
File[] listFiles = file.listFiles();
File[] aDirArray = ContextCompat.getExternalFilesDirs(SDCardExample.this, null);
File path = new File(aDirArray[1], Environment.DIRECTORY_DCIM);
String data=path.toString();
int index=data.indexOf("Android");
String subdata=data.substring(0,index);
File sec=new File(subdata+"DCIM/Camera");
File[] secs=sec.listFiles();
File[] aDirArray = ContextCompat.getExternalFilesDirs(MainActivity.this, null);
File path = new File(aDirArray.length>1?aDirArray[1]:aDirArray[0], Environment.DIRECTORY_DCIM);
like Dave Truby wrote
Here is the solution.
Try it...
First get all files in a list
var file=new Java.IO.File("storage/");
var listOfStorages=file.ListFiles();
var isSDPresent=false;
if(listOfStorages[1].Name.Containes("emulated")||listOfStorages[0].Name.Containes("-"))
{
isSDPresent=true;
var sdCardName=listOfStorages[0].Name;
}
It works.

android-where to store lots of images and how to move them while installing

I'm writing an android app that contains about 500 images .
there are somethings that make me worry, I don't want to use internet.
1-the application size will be very big , is there anyway to moving images to sd card while installing? some devices may don't have this amount of space on the phone .
2-should I make 3 images for hdpi , ldpi and mdpi ?
You can put you image in asset folder. If you want to transfer image from assets to SD Card then you can't do like this.
But you can do by one way. You put your image on server and at 1st time when you will open app you can download it and save it in SD Card and then access from there.
Yes, it will be big. No, you can't remove them from your package.
No, you can make only hdpi images. Android will scale them automatically (which may slow down a bit the app).
Suggestion - use internet. Since the user has internet to download your app, he can wait to download the resources on first start. Also it give you the ability to add/remove files via online configuration. Just imagine if you have to add 1 image and upload new version - this means that the user will have to download the same huge package again.
I had a similar requirement - include a bunch of images in the app, but in my case, the image had to be accessible by any user or app, not just the app that unpacked them. I stored them in the res/raw folder and copied them to user space on start up:
private void loadCopyResources() {
// copy resources to space any activity can use
String sourceName;
String resourceName;
String fileName;
int resource;
String typeName = sourceSink.Types.photo.toString();
for (sourceSink.Sources source: sourceSink.Sources.values() ){
for (int i = 0; i< photoFileCount; i++) {
sourceName = source.toString();
resourceName = sourceName + "_" + typeName + (i+1); // i.e. dropbox_photo2
fileName = resourceName + ".jpg"; // files requires extension
resource = getResources().getIdentifier(resourceName, "raw", "com.example.myapp");
createExternalStoragePublicFile(typeName,fileName, resource); // copy it over
}
}
}
void createExternalStoragePublicFile(String fType, String fname, int res ) {
// Create a path where we will place our picture in the user's
// public pictures directory. Note that you should be careful about
// what you place here, since the user often manages these files. For
// pictures and other media owned by the application, consider
// Context.getExternalMediaDir().
File path = null;
if (((fType.equals(sourceSink.Types.photo.toString())) || (fType.equals(sourceSink.Types.file.toString())) ) ){
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
}
if (fType.equals(sourceSink.Types.music.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
}
if (fType.equals(sourceSink.Types.video.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
}
File file = new File(path, "/" + fname);
try {
// Make sure the Pictures directory exists.
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(res);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
scanMedia(file);
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
sourceSink, which I didn't include, is just a list of file names and file types I needed copied.

Categories

Resources