0

I'm using MediaStore.Images.Media.EXTERNAL_CONTENT_URI with a filter on MediaStore.MediaColumns.DATA and a Cursor to retrieve the images. I need to keep the image path where the images are stored, (which are in sub-folders) because I am using the image path to find the correct images to display them with their parent inventory item. I've tried many different ways with no success except using the MediaStore. This is working. The problem is; the MediaStore.MediaColumn.DATA is deprecated (sigh). So I need to refactor out the deprecated field use.

This is an inventory application where each inventory item has a "Source", "Order Number", "OrderItemNumber" plus other data. I stored the images on the sdCard in a folder I created under "downloads" called "InventoryImages". Under this folder are sub-folders (actual source name), (actual Order Number), (actual Order Item Number). Within the "OrderItemNumber" folder are several images, all for this specific combination.

The Inventory Item data is stored in a sqlite db. ((preloaded)(working on the maintenance pieces)). But I'm really trying to avoid storing the image (blob) in the db. But I am open to any and all suggestions for an alternative. I'd rather not have the images deleted if the application is uninstalled. I currently have 104 inventory items, each with average 4 images.

Again, the included fragment is working, I just need an alternative to the DATA column which the value is the image path.

Fragment:

   public class ListViewFragment extends Fragment 
    implements IAdapterCallback
    {

     public ArrayList<ImageInfo> getAllMediaStoreImages()
     {
        ArrayList<ImageInfo> allImages = new ArrayList<>();
        String inventoryDirectory;
        if(Build.FINGERPRINT.contains("generic"))
            inventoryDirectory = "Inventory"; // <-- use this for the emulator
        else
            inventoryDirectory = "InventoryImages"; // <-- use this for my phone

        Uri uri = 
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String selection = 
    MediaStore.MediaColumns.DATA + " LIKE ? ";
        String[] selectionArgs = new String[]{"%" + 
    inventoryDirectory + "%"};
        Cursor cursor = 
   this.getActivity().getContentResolver().query(uri, 
    null, selection, selectionArgs, null);

        String[] names = cursor.getColumnNames();


        try {
            if (cursor != null) {
                while (cursor.moveToNext())
                {
                    String filePath = 
 
 
 
 cursor.getString(cursor.getColumnIndex((MediaStore.MediaColumns.*DATA*)));
                    String fileName = 
 
 
 
 cursor.getString(cursor.getColumnIndex((MediaStore.MediaColumns.TITLE)));

                    int inventoryDirectoryLoc = 
    filePath.indexOf(inventoryDirectory) + 
    inventoryDirectory.length() + 1;
                    String matchPath = 
    filePath.substring(inventoryDirectoryLoc, 
    filePath.lastIndexOf('/'));
                    allImages.add(new 
    ImageInfo(filePath, fileName, matchPath));
                }
            }
            return allImages;
        }
        catch (Exception x)
        {
            x.printStackTrace();
            return null;
        }
        finally
        {
            if(cursor != null)
                cursor.close();
        }
    }
mikeB
  • 310
  • 3
  • 7
  • You did not tell why you would need a file system path where everybody else would just use the uri to access the image files. – blackapps Jun 11 '21 at 06:57
  • blackapps, I need to get a listing (file names) from a Uri path that I can create (minus the file name). I know the path to the image files but I won't know how many files there will be or what the file names are. But now, I don't think that I need to use the DATA column so I will try using only the TITLE column which has the file name(s). Is there a better way to get the file names from a Uri that I can create that works with minSdkVersion 28? Thanks for getting me in the right direction. I have android brain fog. I'll continue this way without using the deprecated MediaStore Column DATA – mikeB Jun 11 '21 at 23:08
  • This seems to be what I need: public ArrayList getItemInventoryImages(String sourceSpecificPath) { String fullPath = Environment.getExternalStorageDirectory().toString() + "/Download/" + sourceSpecificPath; File file = new File(fullPath); if(!file.isDirectory()) {return null;} if(!file.canRead() {return null;} File[] files = file.listFiles(); return files; } – mikeB Jun 12 '21 at 01:01

1 Answers1

0

This worked:

  public ArrayList<File> getItemInventoryImages(String sourceSpecificPath)
    {
        String sourcePath = Environment.getExternalStorageDirectory().toString() + "/Download/" + sourceSpecificPath;
        File file = new File(sourcePath);
        if(!file.isDirectory()) {return null;}
        if(!file.canRead() {return null;}
        File[] files = file.listFiles();
        return files;
    }

Now I can feed the File array to the Adapter and use Glide to display them.

mikeB
  • 310
  • 3
  • 7
  • Holly Cows, Environment.getExternalStorageDirectory is deprecated. And the documentation points me back to the MediaStore provider. All I want to do is get a very simple directory listing of image files stored in a known path. Why is that so difficult in Android? I know the path. I don't know how many files are in the location and I don't know the file names. It's not a matter of using a Uri or a string. All I need is a Android function to get the File Names for each of the image files. Loading the image files is not a problem. Getting the list of image file names is the problem. – mikeB Jun 15 '21 at 00:52
  • Sorry about all of this. What I actually need is a constant or method to get the Uri or path to the device SD Card Download. I know what it is right now but how safe is it to hard code that? – mikeB Jun 15 '21 at 01:02
  • What a rabbit hole. :-) https://stackoverflow.com/questions/5858107/how-to-get-file-path-from-sd-card-in-android – mikeB Jun 16 '21 at 02:24
  • The link that I posted and stated that it worked ONLY works on an emulator. Is finding the directory path of a storage card on a android device top secret or something? That's all I need. To find the path of the SD Card. Can anyone help with this? I've spent hundreds of hours searching and trying different methods posted. The only one's that work are deprecated. Is there a way that is not deprecated? Please help. – mikeB Jun 25 '21 at 17:47