开发者

Attach an image to a contact

I am having exactly the same problem as described here.

I am trying to use this Intent: android.provider.ContactsContract.Intents.ATTACH_IMAGE

Starts an Activity that lets the user pick a contact to attach an image to.

Sounds suitable to me but unfortunately results in an ActivityNotFoundException.

Code:

import android.provider.ContactsContract;  
...  
try {  
    Intent myIntent = new Intent();  
    myIntent.setAction(ContactsContract.Intents.ATTACH_IMAGE);  
    myIntent.setData(imageUri);  
    startActivity(myIntent);  
} catch (ActivityNotFoundException anfe) {  
    Log.e("ImageContact", 
            "Firing Intent to set image as contact failed.", anfe);  
    showToast(this, "Firing Intent to set image as contact failed.");  
}

I cannot find any error in the code above. The imageUri is correct for following code is working perfectly:

Code:

try {  
    Intent myIntent = new Intent();  
    myIntent.setAction(Intent.ACTION_ATTACH_DATA);
    myIntent.setData(imageUri)开发者_C百科;  
    startActivity(myIntent);  
} catch (ActivityNotFoundException anfe) {  
    Log.e("ImageContact", 
            "Firing Intent to set image as contact failed.", anfe);  
    showToast(this, "Firing Intent to set image as contact failed.");  
}

As mentioned in the link this results in a another menu before getting to the contacts. That is acceptable but not perfect.


If you already know the file path you can use:

values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.DATE_ADDED, currentTime);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, 0);
values.put(Images.Media.DATA, filePath);
values.put(Images.Media.SIZE, size);

getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

This way no nead to open a bitmap stream if you already have the file.


I'm having this problem too. I've had a little more success using by setting the Uri using the following code taken from http://developer.android.com/guide/topics/providers/content-providers.html

However, after selecting a contact and cropping the image the new contact icon still is not set?

// Save the name and description of an image in a ContentValues map.  
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");

// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
    outStream.close();
} catch (Exception e) {
    Log.e(TAG, "exception while writing image", e);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜