【问题标题】:Error while select image from gallery and set as contact photo从图库中选择图像并设置为联系人照片时出错
【发布时间】:2014-05-26 09:17:39
【问题描述】:

点击事件时。那个打开的联系人列表。而选择一个联系人,它包含错误。

启用加载照片

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

【问题讨论】:

    标签: android image performance android-intent uiimageview


    【解决方案1】:

    只需用这条线改变你的线

    Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.id.photo);
    

    到

    Bitmap icon= BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow1);
    

    【讨论】:

    • 对不起,再次包含相同的错误。我没有附上联系人照片。 ""启用加载照片""
    • @Hiteshpatel 只需在 R.drawble.yourimage 中写下图像的名称,它会起作用..它在我的代码中起作用。
    • 当我选择联系人而不是只收到消息时。 ""启用加载照片""
    • stackoverflow.com/questions/23844033/… 勾选此链接。这是我的完整代码。
    • 我同意,因为它在您的电话中正在运行。
    【解决方案2】:

    你应该改变这个

      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

    【讨论】:

    【解决方案3】:

    将此添加到您的意图代码中:

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intent, "Set as:"));
    

    【讨论】:

      猜你喜欢
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 2012-01-02
      • 2017-09-06
      • 1970-01-01
      • 2021-05-10
      相关资源
      最近更新 更多