Select an Image from gallery in ANDROID and show in an ImageView.

By | December 30, 2018

Hi all
In this tutorial I will show you how to get an image from your phone gallery and show it in an imageview.Here we use intents to open up the image gallery and get the image URI.

 
Demo

 
Watch Video Tutorial

 
Steps :

  • Ask storage permission from the user if the app runs on Marshmallow or greater
  • Set the image type as “image” to get only the images.
  • on onActivityResult if the result is OK, then get the data using getData() function and converting the imageURI to the stringPath.
  • Then show the image in the Imageview using setImageURI.

Update manifest Permission

Add the below permission in the Manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Ask Permission to Access Storage

If the App is running in devices which run Android MarshMallow or higher, we need to get the permission from the user before proceeding.

We are going to use the below method to ask the permission and the onRequestPermissionsResult is the callback when the user “Allows” or “Denies” the permission.

 private void handlePermission() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //ask for permission
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    SELECT_PICTURE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case SELECT_PICTURE:
                for (int i = 0; i < permissions.length; i++) {
                    String permission = permissions[i];
                    if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
                        if (showRationale) {
                            //  Show your own message here
                        } else {
                            showSettingsAlert();
                        }
                    }
                }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

Complete Example

package com.androidtutorialprojects;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import tutorials.coderzheaven.com.androidtutorialprojects.R;

public class SelectImageActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int SELECT_PICTURE = 100;
    private static final String TAG = "SelectImageActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_image);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        setTitle("Select Image");

        findViewById(R.id.btn).setOnClickListener(this);

        handlePermission();
    }

    private void handlePermission() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //ask for permission
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    SELECT_PICTURE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case SELECT_PICTURE:
                for (int i = 0; i < permissions.length; i++) {
                    String permission = permissions[i];
                    if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
                        if (showRationale) {
                            //  Show your own message here
                        } else {
                            showSettingsAlert();
                        }
                    }
                }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /* Choose an image from Gallery */
    void openImageChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    }

    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (resultCode == RESULT_OK) {
                    if (requestCode == SELECT_PICTURE) {
                        // Get the url from data
                        final Uri selectedImageUri = data.getData();
                        if (null != selectedImageUri) {
                            // Get the path from the Uri
                            String path = getPathFromURI(selectedImageUri);
                            Log.i(TAG, "Image Path : " + path);
                            // Set the image in ImageView
                            findViewById(R.id.imgView).post(new Runnable() {
                                @Override
                                public void run() {
                                    ((ImageView) findViewById(R.id.imgView)).setImageURI(selectedImageUri);
                                }
                            });

                        }
                    }
                }
            }
        }).start();

    }

    /* Get the real path from the URI */
    public String getPathFromURI(Uri contentUri) {
        String res = null;
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        cursor.close();
        return res;
    }

    @Override
    public void onClick(View v) {
        openImageChooser();
    }

    private void showSettingsAlert() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("App needs to access the Camera.");
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "DONT ALLOW",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //finish();
                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "SETTINGS",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        openAppSettings(SelectImageActivity.this);
                    }
                });
        alertDialog.show();
    }

    public static void openAppSettings(final Activity context) {
        if (context == null) {
            return;
        }
        final Intent i = new Intent();
        i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        i.setData(Uri.parse("package:" + context.getPackageName()));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(i);
    }

}


Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.androidtutorialprojects.SelectImageActivity"
    tools:showIn="@layout/activity_select_image">

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Image" />

</LinearLayout>

Note : if you are getting issues with the path selected from photo or Gallery, you can check this post to fix it.

Source Code

You can download the complete Android Studio Source Code from here.

Please send your comments to coderzheaven@gmail.com.

59 thoughts on “Select an Image from gallery in ANDROID and show in an ImageView.

  1. Prashant Wankhede

    Thank u , it works in my application successfully.

    Reply
  2. Prashant Wankhede

    thank you, above code works in my application.

    Reply
  3. idoudi

    thank you very much for this tuto…:)))
    but I have a problem with the size of the imageView. It does not take the space I have reserved in xml file.
    can u help me please…

    Reply
    1. James

      Can’ t really understand the question. I think the problem may be because your image resolution is too small to fit in the provided xml size.

      Reply
      1. idoudi

        Exactly. it is a problem of my image resolution..Thank u James for your help…

        Reply
  4. remy215

    You’re the man! I’ve spent hours trying to figure out why I could not open file based on value returned by data.getPath(). This is in fact because it is not the real file path and I was missing all of the projection & cursor stuff.
    Thanks a lot.
    Remy

    Reply
  5. Pingback: android: cannot load external pictures [solved] « Willing wheels

  6. dabious

    Code works perfect, however after selecting a photo if i click my add photo button again and select a different photo the app force closes . Any way to make this work?

    Reply
  7. dabious

    Code works great but I am running into this issue.. external allocation too large for this process. Any tutorials on how to implement a down sampling to the images that works with your code above?

    Reply
  8. vineet

    images store in sd card and open gallary
    if click on picture then give message(unfortunately camera has stopped)

    Reply
  9. vineet

    When your code is run then give same message un fortunately camera has stopped.
    please help me
    Please reply as soon as possible

    Reply
    1. James

      Hey vineet, please check the logcat and paste here the reason for this problem. then only I can help, because this is a perfectly working code.

      Reply
  10. Uğur Yaşar

    Thanks a lot. This example is exactly what am i looking.

    Reply
  11. Christina

    Hi, this works perfectly. But, how if i want to save it into database? can you show me the codes? i’ve spent days on this problems.
    Thanks in advance. Appreciate.

    Reply
    1. James

      Don’t save the image to the database, but save it to the SDCARD or the application sandbox and save the corresponding path in the database.

      Reply
  12. Pitr

    Hey this code is pretty cool
    But the only problem I have is
    “The images I take from camera of my application does not display by this image picker”
    Can you help me here?
    Thanx in advance

    Reply
  13. falah

    hi.i have got error in this two line
    1.import android.provider.MediaStore;
    2. private static final int SELECT_PICTURE = 1;
    please help me…

    Reply
  14. Meenakshi

    Hi, this works perfectly, bt i want to show the selected image into the next layout.. plz help me..

    Reply
    1. James

      You will get the path of the image just pass it with putExtra to the next activity and show it in the same way. that’s all.

      Reply
  15. jalil

    Hi,
    From API 11
    the “managedQuery()” method is depricated
    so does any one have any isea as how the same program can be done for API 11 and above

    Eclipse adiced to use CursorLoader
    but i dont know how to use it

    Reply
  16. Aisha

    thanks for nice info. 🙂
    Ngg.. I’m a student and start learn android,
    I tried to use this code to take two different picture from gallery and display it into two different imageview.
    But it can’t work. I just success to display first image, but not for the second.
    Do you know why is it?

    Reply
    1. James

      Are you setting in the imageview correctly. check once more. it should work with any image.please check your pic request id also.

      Reply
  17. sri

    Hi, I have used this code to upload a picture in the header of list view in the Side Menu Navigation. It works fine but, when I close the application, the image disappears. Is there any solution for that?

    Reply
  18. Ankit

    Image Path value is showing null in it. when you sysout in your program

    Reply
  19. gayathri

    Hey there, i am having the same prob, which say the process has stopped. Cn i check if the ” Cursor cursor = managedQuery(uri, projection, null, null, null);” needs to be deprecated?

    Reply
  20. faffou

    Thank you for this tuto, , it works successfully, but i need the same work for an xml file. I mean i want to open and read an xml file. the problem is that i can’t see my file in the galery. Have you any suggestion ??

    Reply
    1. James

      Gallery is for images and videos only, not for other types of files.
      Install a file explorer app like ES file explorer and there going to right path you can see the XML file.

      I think your question is “you want to select an xml file by opening a file explorer” Isn’t it?
      Then you have to make a file browser or Check in Google for Libraries like that.
      I am sure You will find more than one.

      Thanks

      Reply
      1. faffou

        The problem is that when i click to the button to browse my phone contents, i can see only: galerie, contacts, photos and the MP3 player, so how can i access to the other folders.
        Thinks

        Reply
        1. faffou

          Thank you James. I installed the explorer, and now i can see all files.

          Reply
  21. Wiga

    The Application closed when i select a image. Can help me ?

    Reply
    1. James

      Please reply with the error you are getting in the logcat.

      Reply
  22. Harin Kaklotar

    wow this code so simple to understand….great work:)

    Reply
  23. Niamh

    Hi this code works great, so thank you for that! However I want to be able to select two different images and then use these images for other processing methods. I have tried multiple ways to enhance this code to allow me to do this but haven’t been successful so far. Could you give me any advice on this?

    Reply
  24. priyance

    This code is working up to the time the application is entering into gallery but as i select image which is to be displayed in imageview, the application is getting crashed. I dont know where the problem is. this is the logcat that is being generated.

    java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
    at android.app.ActivityThread.access$900(ActivityThread.java:175)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5602)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
    at dalvik.system.VMRuntime.pauseGc(Native Method)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
                at android.app.ActivityThread.access$900(ActivityThread.java:175)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:146)
                at android.app.ActivityThread.main(ActivityThread.java:5602)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)
    08-13 22:43:10.536 8397-8397/com.collegeproject.prsample D/dalvikvm﹕ GC_FOR_ALLOC freed 80K, 13% free 7696K/8760K, paused 13ms, total 13ms
    08-13 22:43:10.556 8397-8397/com.collegeproject.prsample I/dalvikvm-heap﹕ Grow heap (frag case) to 20.473MB for 12582928-byte allocation
    08-13 22:43:10.566 8397-8406/com.collegeproject.prsample D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 6% free 19983K/21052K, paused 12ms, total 12ms
    08-13 22:43:11.041 8397-8397/com.collegeproject.prsample D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
    08-13 22:43:11.046 8397-8397/com.collegeproject.prsample D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
    08-13 22:43:11.051 8397-8397/com.collegeproject.prsample D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
    08-13 22:43:11.056 8397-8397/com.collegeproject.prsample E/﹕ Device driver API match
    Device driver API version: 23
    User space API version: 23
    08-13 22:43:11.056 8397-8397/com.collegeproject.prsample E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
    08-13 22:43:11.121 8397-8397/com.collegeproject.prsample D/OpenGLRenderer﹕ Enabling debug mode 0
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:11.126 8397-8397/com.collegeproject.prsample W/ViewRootImpl﹕ Dropping event due to no window focus
    08-13 22:43:39.056 8593-8593/com.collegeproject.prsample I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
    08-13 22:43:39.081 8593-8593/com.collegeproject.prsample I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-N7100_4.4.2_0040
    08-13 22:43:39.081 8593-8593/com.collegeproject.prsample I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
    08-13 22:43:39.081 8593-8593/com.collegeproject.prsample D/dalvikvm﹕ Late-enabling CheckJNI
    08-13 22:43:39.311 8593-8593/com.collegeproject.prsample W/dalvikvm﹕ No implementation found for native Ldalvik/system/VMRuntime;.pauseGc:(Ljava/lang/String;)I
    08-13 22:43:39.316 8593-8593/com.collegeproject.prsample E/ActivityThread﹕ Pause GC
    java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
    at android.app.ActivityThread.access$900(ActivityThread.java:175)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5602)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
    at dalvik.system.VMRuntime.pauseGc(Native Method)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
                at android.app.ActivityThread.access$900(ActivityThread.java:175)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:146)
                at android.app.ActivityThread.main(ActivityThread.java:5602)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)
    08-13 22:43:39.381 8593-8593/com.collegeproject.prsample D/dalvikvm﹕ GC_FOR_ALLOC freed 103K, 13% free 7722K/8808K, paused 16ms, total 16ms
    08-13 22:43:39.381 8593-8593/com.collegeproject.prsample I/dalvikvm-heap﹕ Grow heap (frag case) to 9.567MB for 1120016-byte allocation
    08-13 22:43:39.406 8593-8602/com.collegeproject.prsample D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 11% free 8815K/9904K, paused 25ms, total 25ms
    08-13 22:43:39.526 8593-8593/com.collegeproject.prsample D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
    08-13 22:43:39.531 8593-8593/com.collegeproject.prsample D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
    08-13 22:43:39.536 8593-8593/com.collegeproject.prsample D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
    08-13 22:43:39.546 8593-8593/com.collegeproject.prsample E/﹕ Device driver API match
    Device driver API version: 23
    User space API version: 23
    08-13 22:43:39.546 8593-8593/com.collegeproject.prsample E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
    08-13 22:43:39.641 8593-8593/com.collegeproject.prsample D/OpenGLRenderer﹕ Enabling debug mode 0

    Reply
  25. mallz

    I am getting the below error only in MI mobile

    UncaughtException: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Camera/IMG_20170820_161028.jpg typ=image/jpeg flg=0x1 }} to activity {com.henotetech.mallzapp/com.henotetech.mallzapp.userprofile.UserProfileeditActivity}: java.lang.NullPointerException: Attempt to invoke interface method ‘boolean android.database.Cursor.moveToFirst()’ on a null object reference
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3744)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3787)
    at android.app.ActivityThread.access$1500(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:5529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
    Caused by: java.lang.NullPointerException: Attempt to invoke interface method ‘boolean android.database.Cursor.moveToFirst()’ on a null object reference
    at com.henotetech.mallzapp.userprofile.UserProfileeditActivity.getPathFromURI(UserProfileeditActivity.java:559)
    at com.henotetech.mallzapp.userprofile.UserProfileeditActivity.onSelectFromGalleryResult(UserProfileeditActivity.java:521)
    at com.henotetech.mallzapp.userprofile.UserProfileeditActivity.onActivityResult(UserProfileeditActivity.java:740)
    at android.app.Activity.dispatchActivityResult(Activity.java:6508)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3740)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3787) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:153) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:5529) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 

    Reply
  26. Anurag

    Hi, can you provide me the code to upload multiple images using flags.
    I tried using many variations but it kept crashing.

    Reply
  27. Pingback: Path from URI returning Null when selecting from Photos or Gallery in Android – Solved – CoderzHeaven

  28. Dinesh

    Hey, how can i set the validation for image uploading. I want my image size should be 1mb only. else want to show some alert message.

    Reply
    1. James

      Please check the visibility of your imageview and the dimensions.

      Reply
  29. Pingback: get selected image from gallery into imageview

  30. Pingback: Android: How to display selected image gallery in ImageView?

Leave a Reply

Your email address will not be published. Required fields are marked *