0

I know how to retrieve image from sdcard but i can't find solution of retrieving specific image which contains unique timestamp with its name.

How can i get image from sdcard which has unique timestamp while i storing into External directory.?

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();

File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);

How can i retrieve above image from sdcard?

Shreeya Chhatrala
  • 1,441
  • 18
  • 33

1 Answers1

0

This below code is to retrieve the files from the folder with filename start with "QR_[eight int '-' six int]"

private void getFiles() {
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");

    String path = imagesFolder.getAbsolutePath();

    final String where = MediaStore.Images.Media.DATA + " like ? ";
    final String[] args = new String[]{path + File.separator + "QR_" + "%"};
    final String[] columns = {MediaStore.Images.Media.DATA};
    final Cursor cursor = getActivity().getContentResolver().query(uri, columns, where, args, null);
    final String pattern = Pattern.quote(path) + "/[^/]*";

    if (cursor != null && cursor.moveToFirst()) {

        do {
            final String curPath = cursor.getString(0);
            if (curPath.matches(pattern)) {
                final File file = new File(curPath);
                    if(file.getName().matches("\\d{8}-\\d{6}")) {
                        // your file
                    }
            }
        } while (cursor.moveToNext());
        cursor.close();
    }
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41