将图像附加到联系人

时间:2011-02-02 19:51:14

标签: android image contact

我遇到与here所述完全相同的问题。

我正在尝试使用此Intent: android.provider.ContactsContract.Intents.ATTACH_IMAGE

Starts an Activity that lets the user pick a contact to attach an image to.
听起来很合适,但很遗憾会产生ActivityNotFoundException

代码:

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.");  
}

我在上面的代码中找不到任何错误。 imageUri对于以下代码完美运行是正确的:

代码:

try {  
    Intent myIntent = new Intent();  
    myIntent.setAction(Intent.ACTION_ATTACH_DATA);
    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.");  
}

如链接中所述,这会在到达联系人之前生成另一个菜单。这是可以接受但不完美的。

2 个答案:

答案 0 :(得分:1)

如果您已经知道可以使用的文件路径:

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

如果您已经拥有该文件,这样就无法打开位图流。

答案 1 :(得分:0)

我也有这个问题。通过使用从http://developer.android.com/guide/topics/providers/content-providers.html

获取的以下代码设置Uri,我获得了更多成功

但是,选择联系人并裁剪图像后,仍未设置新的联系人图标?

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