Tuesday, December 19, 2017

Picking a picture either from photos or multimedia, but not by recent images

Leave a Comment

I would like to pick picture similar to whatsapp:

Code:

   public void pickImage()     {         if (Build.VERSION.SDK_INT <19)         {             Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);               Intent intent = new Intent();             intent.setType("image/*");             intent.setAction(Intent.ACTION_GET_CONTENT);             startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);         } else {             Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);             intent.addCategory(Intent.CATEGORY_OPENABLE);             intent.setType("image/*");             startActivityForResult(intent, KITKAT_SELECT_PICTURE);          }     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         super.onActivityResult(requestCode, resultCode, data);          if (resultCode != Activity.RESULT_OK) return;         if (null == data) return;         Uri originalUri = null;         if (requestCode == SELECT_PICTURE)         {             originalUri = data.getData();         }         else if (requestCode == KITKAT_SELECT_PICTURE)         {             originalUri = data.getData();             final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);             // Check for the freshest data.             getContentResolver().takePersistableUriPermission(originalUri, takeFlags);         }     } 

Question:

The above can pick pictures and then showing pic properly in an imageview. However, during the picking process, it messes up the pictures photoed by camera (called album) and pictures in other media, and is sorted by most recent images.

How could i make it similar to whatsapp, picking image from a folder containing only but all images taken from camera, or a folder containing all other multimedia, INSTEAD OF messing up camera photos and mutimedia pictures by most recently added?

Thank you.

2 Answers

Answers 1

Use MediaStore.Images.Media.query function with order by set DSC DATE_ADDED field. You can set content URI for internal/external storage based on your requirement.

Answers 2

Try this hope so it will be helpful my problem was fix out with help of this code..

Uri selectedImageURI = data.getData(); imageFile = new File(getRealPathFromURI(selectedImageURI)); 

and:

private String getRealPathFromURI(Uri contentURI) {     String result;     Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);     if (cursor == null) { // Source is Dropbox or other similar local file path         result = contentURI.getPath();     } else {          cursor.moveToFirst();          int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);          result = cursor.getString(idx);         cursor.close();     }     return result; } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment