Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

app crashes when we access from phonebook #19

Open
KarunJose opened this issue Aug 28, 2019 · 7 comments
Open

app crashes when we access from phonebook #19

KarunJose opened this issue Aug 28, 2019 · 7 comments

Comments

@KarunJose
Copy link

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

@ktamilvanan
Copy link

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

@zhukeev
Copy link

zhukeev commented Feb 11, 2020

The same

@thesanjeevsharma
Copy link

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

@galvanu
Copy link

galvanu commented Feb 17, 2020

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

@rt16
Copy link

rt16 commented Mar 23, 2020

I'm too facing this issue

@nelsonBlack
Copy link

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
Copy link

linhobs commented Sep 27, 2021

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants