从图库中选择图像并设置为联系人照片时出错

时间:2014-05-26 09:17:40

标签: android image performance android-intent uiimageview

单击事件时

。打开联系人列表。当一个联系人选择其包含错误。

  

启用加载照片

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

3 个答案:

答案 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:"));