。打开联系人列表。当一个联系人选择其包含错误。
启用加载照片
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Context context= getApplicationContext();
Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.id.photo);
try
{
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_ATTACH_DATA);
myIntent.setType("image/jpeg");
myIntent.putExtra("mimeType", "image/jpg");
myIntent.putExtra(Intent.EXTRA_STREAM, icon);
startActivity(myIntent);
}
catch (ActivityNotFoundException e)
{
// e.printStackTrace();
Log.i("ImageContact",
"Firing Intent to set image as contact failed.", e);
}
}
这是我的ActivityResult方法。
if(resultCode == RESULT_OK){
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
photo1.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
你应该改变这个
Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.id.photo);
要
Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.drawable.photo);
你错了R.id.photo
。你需要使用R.drawable.photo
访问drawable。
更新:尝试这种方式
Context context= getApplicationContext();
Bitmap icon= BitmapFactory.decodeResource(context.getResources(),
R.drawable.photo);
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_ATTACH_DATA);
myIntent.setType("image/jpeg");
myIntent.putExtra(Intent.EXTRA_STREAM, icon);
startActivity(myIntent);
并添加
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
在manifest.xml
答案 1 :(得分:1)
只需用此行更改您的行
Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.id.photo);
到
Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow1);
答案 2 :(得分:0)
将此添加到您的意图代码中:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Set as:"));