App crash when picking a contact from REQUEST_CONTACTPICKER - java

Following this solution I have run into an issue with the code presented. The app forces a close every time I pick a contact from the list that pops up. Something tells me the culprit has something to do with this which I found in logcat.
Here is my version of the code:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NUMBER));
name1.setText(name);
num1.setText(number);
}
break;
}
}
Take a look at the logcat

Related

Error When Trying To Pick A contact and Show its Number in a TextBox in Android

First of all sorry for my Bad English
i'm new To Android development.
I'm trying to develop this App in which user can click on a button and the Contacts will show up and then can pick one and only one one of the Contacts and show its number in a textbox.
But i face this Error every time :
"Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor
is initialized correctly before accessing data from it"
and The Funny thing is when i use HAS_PHONE_NUMBER it returns 1 which means the Contact has Phone Number.
and when i use DISPLAY_NAME it returns contact name without any problem.
but when i use NUMBER that Error shows Up!
can anyone help me?
BTW i use Fragment if it helps..?
and this is my code :
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnContact) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT); }
}
private Uri uriContact;
private String contactID; // contacts unique ID
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor phone = getActivity().getContentResolver().query(contactData, null, null, null, null);
phone.moveToFirst();
if (phone.moveToFirst()) {
String num = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// Todo something when contact number selected
placeholderNumber.setText("" + num);
}
}
break;
}
}

FileNotFoundException when getting thumbnail from video ANDROID

I have a problem when I'm trying to get video thumbnail from phone gallery videos. I have a method which is getting bitmap of video thumbnail,but this method is not working perfectly. The minus of mine method is that,for some videos it's not working and I'm getting "FileNotFoundException",but for some videos this method is working fine.
private void pickVideosFromGallery() {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_VIDEO);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK){
return;
}
switch (requestCode)
{
case SELECTED_VIDEO:
Uri uri = data.getData();
String[] projection = {MediaStore.Video.VideoColumns.DATA};
Cursor cursor = getContext().getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bit = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
break;
}}
07-16 20:11:49.647 32387-32387/com.example.tstv.keepfavoritevideos E/folderFragment: Video Path: android.graphics.Bitmap#71d9de1
07-16 20:12:49.992 32387-32387/com.example.tstv.keepfavoritevideos E/MediaMetadataRetriever: setDataSource - FileNotFoundException
07-16 20:12:49.992 32387-32387/com.example.tstv.keepfavoritevideos E/folderFragment: Video Path: null
As you can see from that LOG, for some videos its working, and I am getting bitmap,but for some its not working. WHY?

ZXing Scanner Searching for Results

I am having some issues with ZXing's barcode scanner in my android application. I asked a question here yesterday that explains a null pointer error that I was having. The issue was solved and just showed that the cursor wasnt retrieving any information back.
My issues is that I know query is correct and I know the code is correct because I have tested it out without the scanner separately and it all works fine. Is it possible that I am calling to the database wrong in the barcode scanner intent or is there a certain way that I should be doing it?
Any help or guidance would be great!
onActivity Result
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//get the extras that are returned from the intent
barcode_number = intent.getStringExtra("SCAN_RESULT");
String bNumber = barcode_number.toString();
//Code to add scanned item to DB, needs barcode data before can be ruin
final Cursor barInformation = adapter.barcodeInfo(barcode_number);
barInformation.moveToFirst();
String itemName = barInformation.getString(barInformation.getColumnIndex("barcode_item_name"));
Toast toast = Toast.makeText(this, "Barcode Number:" + itemName ,Toast.LENGTH_SHORT);
toast.show();
}
}
}
Query
public Cursor barcodeInfo(String number){
// Safe check to make sure db and number is not null
if(db == null || number == null){
return null;
}
return db.rawQuery("select _id, barcode_item_name, barcode_measurement, barcode_unit from barcode where barcode_number = ?", new String[]{number});
}
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime: at com.example.rory.prototypev2.DBMain.barcodeInfo(DBMain.java:437)
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime: at com.example.rory.prototypev2.scanner.onActivityResult(scanner.java:77)
Try like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String strData = scanResult.getContents(); // use this
} else {
Toast.makeText(this, "Error message", Toast.LENGTH_LONG).show();
}
break;
}
}
}
I have fixed the issue. My problem was that I wasnt opening the database before I was calling the functions and thus the function was a null object. The correct code is as below.
adapter.open(); // I was missing this line
Cursor c = adapter.barcodeInfo(bNumber);
c.moveToPosition(-1);
while(c.moveToNext())
{
name = c.getString(c.getColumnIndex("barcode_item_name"));
measurement = c.getInt(c.getColumnIndex("barcode_measurement"));
unit = c.getString(c.getColumnIndex("barcode_unit"));
Toast toast = Toast.makeText(this, "Adding" + name + "to Kitchen", Toast.LENGTH_SHORT);
toast.show();
}

How to add contact in Android like skype, whatsapp in native contact app?

I am creating a contact app but want to add contacts in the native android contact app from my app just like Skype or WhatsApp. What class would I need to extend to implement this functionality?
Here is the picture of exactly what I want to create:
Ok if I understand what you are looking for. You want to use the native Android contact list.. If so then here are the steps:
fire intent for result
receive the intent for result looking for the intent result code.
return a cursor with the contact information
set the values.
A short example. fire intent
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
//Receive the result
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
//deal with the resulting contact info. I built a separate util class for that.. but here is an example of the code.
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Email.ADDRESS };
Uri result = data.getData();
String id = result.getLastPathSegment();
ContentResolver contentResolver = getActivity().getContentResolver();
//return cursor
cur = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
ContactsContract.CommonDataKinds.Phone._ID + " like \"" + idUser + "%\"", null, null);
//Use the cursor to return what you need.
}
}
Here is an example call to the cursor. Please read some more about the contact cursor in the android docs.
email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

Android: App crashes when selecting image from Samsung gallery (but it works with Google Photo app)

Good day,
The following code works when I select an image from the Google Photo app. However, when I select the same image from Samsung stock gallery, my app crashes.
I did some troubleshooting and realize the problem is with the getContentResolver().query():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
//Error happens when program try to run the following line of code
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_LONG).show();
}
}
Please help me. Thank you.

Categories