简体   繁体   English

将图像附加到联系人

[英]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 我正在尝试使用以下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 . 听起来很适合我,但不幸的是导致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: imageUri对于以下代码可以正常工作是正确的:

Code: 码:

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

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 通过使用以下来自http://developer.android.com/guide/topics/providers/content-providers.html的代码设置Uri,我获得了一些成功

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM