“当前无法下载。请稍后重试”尝试从Android应用中的图库加载图片时

时间:2013-08-05 11:02:59

标签: android gallery quickcontactbadge

我的应用与联系人应用相同。在这里,我将添加联系人详细信息,如电话号码,来自画廊的人物照片等。我已经采取了一个quickcontactbadge,当clciked将用户带到电话库并让用户选择他想要的图片。

直到这里我的所有代码都运行正常。当我点击特定图像时,它会显示此Toast消息“目前无法下载。请稍后再试”,应用已关闭。我尝试使用相同的图像在手机通讯录应用程序。它装了。

这是我的代码,

  // 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);
  }

请参阅此图片,

enter image description here

我不知道故障是手机还是我的代码。

这是我的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)

任何人请澄清或建议我一些解决方案。

任何帮助表示赞赏!!感谢。

1 个答案:

答案 0 :(得分:1)

请尝试使用此方法:

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;
}

然后只需致电

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

我不知道您的Intent定义中是否有任何错误,但在我的应用中我确实喜欢这个:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE);