0

我有一个与联系人应用程序相同的应用程序。在这里,我将添加联系方式,例如电话号码、画廊中的人物照片等。我已经制作了一个快速联系徽章,当点击它时,它会将用户带到电话画廊并让用户选择他想要的图片。

到这里我所有的代码都可以正常工作。当我单击特定图像时,它显示此吐司消息“当前无法下载。请稍后再试”并且应用程序已关闭。我尝试在手机通讯录应用中使用相同的图像。它加载了。

这是我的代码,

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

请看这张图片,

在此处输入图像描述

不知道是手机问题还是密码问题。

这是我的日志猫,

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)

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

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

4

1 回答 1

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);
于 2013-08-05T11:09:18.867 回答