"Currently unable to download. Please try again later" when trying to load image from gallery in android app

StackOverflow https://stackoverflow.com/questions/18056606

سؤال

I have an app same as to contacts app. Here, I will be adding contact details like phone number, person photo from gallery etc. I have taken a quickcontactbadge, which, when clciked takes the user to the phone gallery and lets the user to select the pic he want.

Till here all my code works fine. When I click the particular image, It shows this toast message "Currently unable to download. Please try again later" and app is closed. I tried in the phone contacts app with the same image. It loaded.

Here is my code,

  // When Gallery option is selected from menu.
  public boolean onContextItemSelected(MenuItem item) {

    super.onContextItemSelected(item);
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();
    int menuItemIndex = item.getItemId();
    if (item.getTitle().toString().equalsIgnoreCase("Album")) {
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }
    return true;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

                 //set selected image to quickcontactbadge
        qcbaddcustomer
                .setImageBitmap(BitmapFactory.decodeFile(picturePath));
        // String picturePath contains the path of selected Image
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

Please see this Image,

enter image description here

I dont know if the fault is with the phone or my code.

This is my logcat,

08-05 16:43:48.976: E/AndroidRuntime(14257): FATAL EXCEPTION: main
08-05 16:43:48.976: E/AndroidRuntime(14257): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat= }} to activity {com.sriniinfotech/com.sriniinfotech.AddCustomers}: java.lang.NullPointerException
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.os.Looper.loop(Looper.java:130)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at java.lang.reflect.Method.invoke(Method.java:507)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at dalvik.system.NativeStart.main(Native Method)
08-05 16:43:48.976: E/AndroidRuntime(14257): Caused by: java.lang.NullPointerException
08-05 16:43:48.976: E/AndroidRuntime(14257):    at com.sriniinfotech.AddCustomers.onActivityResult(AddCustomers.java:363)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
08-05 16:43:48.976: E/AndroidRuntime(14257):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)

Anyone please clarify or suggest me some solution.

Any help is appreciated!! Thanks.

هل كانت مفيدة؟

المحلول

Try using this method instead:

public String getPath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        final String imagePath = cursor.getString(column_index);
        return imagePath;
}

And then just call

 Uri selectedImage = data.getData();
 String picturePath = getPath(selectedImage);

I don't know if there is anything wrong in your Intent definition, but here in my app I did like this:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top