2

我遇到了与此处描述的完全相同的问题。

我正在尝试使用此意图: 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.");  
}

如链接中所述,这会导致在访问联系人之前出现另一个菜单。这是可以接受的,但并不完美。

4

2 回答 2

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

如果您已经拥有该文件,则无需打开位图流。

于 2011-02-03T21:58:25.003 回答
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);
}
于 2011-02-03T11:39:53.773 回答