2011-02-02 36 views
2

我遇到了与here所述完全相同的问题。将图像附加到联系人

我试图用这个意图: 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."); 
} 

正如这导致了另一个菜单得到的接触前链路提及。这是可以接受的,但并不完美。

回答

0

我也有这个问题。通过设置Uri使用下面的代码取得更多的成功,使用以下代码:http://developer.android.com/guide/topics/providers/content-providers.html

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

// 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

它似乎可以正常工作1.6,2.1,2.2 - 2.0.1似乎有一个错误是联系人图片无法从画廊图像手动设置。 – App8ite 2011-02-03 16:03:03

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

这样没有NEAD打开一个位图流,如果你已经拥有的文件。

相关问题