goderbauer / contact_picker

A Flutter plugin for picking a contact from the address book.
BSD 3-Clause "New" or "Revised" License
77 stars 109 forks source link

app crashes when we access from phonebook #19

Open KarunJose opened 5 years ago

KarunJose commented 5 years ago

while we click contacts from phonebook, the app suddenly crashed. But there was no issue in the previous versions

ktamilvanan commented 4 years ago

same here. When i select the contact on android, the app crashes. WOrks on ios.

zhukeev commented 4 years ago

The same

thesanjeevsharma commented 4 years ago

Same issue. I get the contact in terminal, though.

galvanu commented 4 years ago

Facing the same issues. Does anyone found a solution for this?

rt16 commented 4 years ago

I'm too facing this issue

nelsonBlack commented 4 years ago

this worked for me As per stackoverflow answer -You need to edit the following file C:\your pub-cache directory\contact_picker-0.0.1+2\android\src\main\java\net\goderbauer\flutter\contactpicker\ContactPickerPlugin.java and replace with the following code. https://stackoverflow.com/questions/60566037/flutter-contact-picker `

// Copyright 2017 Michael Goderbauer. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package net.goderbauer.flutter.contactpicker;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import java.util.HashMap;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import static android.app.Activity.RESULT_OK;

public class ContactPickerPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener {
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "contact_picker");
    ContactPickerPlugin instance = new ContactPickerPlugin(registrar.activity());
    registrar.addActivityResultListener(instance);
    channel.setMethodCallHandler(instance);
  }

    private ContactPickerPlugin(Activity activity) {
        this.activity = activity;
    }

  private static int PICK_CONTACT = 2015;

  private Activity activity;
  private Result pendingResult = null;

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("selectContact")) {
      if (pendingResult != null) {
        pendingResult.error("multiple_requests", "Cancelled by a second request.", null);
        pendingResult = null;
      }
      pendingResult = result;

      Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
      activity.startActivityForResult(i, PICK_CONTACT);
    } else {
      //result.notImplemented();
     pendingResult = null;
    }
  }

  @Override
  public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode != PICK_CONTACT) {
      return false;
    }
    if (resultCode != RESULT_OK) {
      pendingResult.success(null);
      pendingResult = null;
    }
    if(pendingResult != null){
    Uri contactUri = data.getData();
    Cursor cursor = activity.getContentResolver().query(contactUri, null, null, null, null);
    cursor.moveToFirst();

    int phoneType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
    String label = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(activity.getResources(), phoneType, customLabel);
    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    String fullName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

    HashMap phoneNumber = new HashMap();
    phoneNumber.put("number", number);
    phoneNumber.put("label", label);

    HashMap contact = new HashMap();
    contact.put("fullName", fullName);
    contact.put("phoneNumber", phoneNumber);

    pendingResult.success(contact);
    pendingResult = null;
    return true;
  }
  else{
    pendingResult = null;
    return false;
  }
  }
}`
linhobs commented 3 years ago

having the same issue. it was working fine untill it suddenly started breaking