我有完全相同的问题描述这里
I am having exactly the same problem as described here.
我试图用这个意图: android.provider.ContactsContract.Intents.ATTACH_IMAGE
开始的活动,可以让用户选择一个联系人的图像附加。
结果听起来很适合我,但遗憾的是导致一个 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.");
}
我找不到在code以上的任何错误。在 imageUri
是正确的以下code为正常使用:
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);
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);
这样没有NEAD打开一个位图流,如果你已经有一个文件。
This way no nead to open a bitmap stream if you already have the file.