1

By using following code I am getting image path in 4.4.4 but, when I am using it in lollipop 5.0.1, it is not working.

String[] proj = {
                MediaStore.Images.Thumbnails._ID,
                MediaStore.Images.Thumbnails.DATA,
        };

        CursorLoader cursorLoader = new CursorLoader(
        this,  MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);        

        Cursor imageCursor = cursorLoader.loadInBackground();
edmz
  • 8,220
  • 2
  • 26
  • 45
allen. Y
  • 11
  • 2
  • Check this answer of so. http://stackoverflow.com/questions/14424624/using-cursorloader-with-loadermanager-to-retrieve-images-from-android-apps – Chinmoy Debnath Aug 03 '15 at 09:57
  • Bear in mind that there is no requirement that `MediaStore` give you a path to a file that you can use. After all, the file may be on [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html), which you cannot access on Android 4.4 and higher. – CommonsWare Aug 03 '15 at 10:21

1 Answers1

0

Start the loader manager by invoking getSupportLoaderManager when it is needed.

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            imageUri = data.getData();
            getSupportLoaderManager().initLoader(0, null, this);
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(this, "Action canceled.", Toast.LENGTH_LONG).show();
        } else { 
            Toast.makeText(this, "Action failed!", Toast.LENGTH_LONG).show();
        } 
    } 
} 

Then create a cursor loader that is used to retrieve the image path.

@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {
            MediaStore.Images.Media.DATA
    }; 
    CursorLoader cursorLoader = new CursorLoader(this, imageUri, projection, null, null, null);

    return cursorLoader; 
} 

When the cursor loader is finished it uses the retrieved data to update the UI.

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if (data != null) {
        int columnIndex = data.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        data.moveToFirst();
        imagePath = data.getString(columnIndex);
    } else { 
        imagePath = imageUri.getPath(); 
    } 

    setupImageView(); 
} 
Srikanth K
  • 190
  • 3
  • 12
  • How to start an activity for result?? [link](http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android) – Srikanth K Aug 03 '15 at 10:50