2015-05-05 37 views
0

我已經創建了由自定義適配器處理的自定義列表視圖。此列表中的每個項目都顯示一個圖像和一個分享按鈕。圖像從離子源加載。這工作正常。如何在自定義列表視圖中共享外部圖像?

現在我想分享圖像時,用戶點擊按鈕。雖然我能夠分享文本等我不能共享這些外部圖像,實現這個代碼後也:Sharing Remote Images

兩件事情:

  • 我是新到Android所以我使用的可能是setTag/getTag完全錯誤
  • if (drawable instanceof BitmapDrawable)是假的,但我不知道爲什麼或如何解決這一問題

任何幫助或建議,非常感謝!

這是我的適配器代碼:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 

    if(convertView == null) { 
     //brand new 
     convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null); 
     holder = new ViewHolder(); 
     holder.contentImageView = (ImageView) convertView.findViewById(R.id.contentImageView); 
     holder.contentLabel = (TextView) convertView.findViewById(R.id.contentLabel); 
     holder.contentShareButton = (Button) convertView.findViewById(R.id.contentShareButton); 

     convertView.setTag(holder); 
    } 
    else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    Content content = mContents[position]; 

    Ion.with(mContext) 
      .load(content.getSrc()) //the external image url 
      .intoImageView(holder.contentImageView); 

    holder.contentLabel.setText(content.getTitle()); 

    holder.contentShareButton.setTag(holder.contentImageView); 
    holder.contentShareButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ImageView ivImage = (ImageView) v.getTag(); 

      //ImageView ivImage = (ImageView) findViewById(R.id.ivResult); 
      // Get access to the URI for the bitmap 
      Uri bmpUri = getLocalBitmapUri(ivImage); 
      if (bmpUri != null) { 
       // Construct a ShareIntent with link to image 
       Intent shareIntent = new Intent(); 
       shareIntent.setAction(Intent.ACTION_SEND); 
       shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
       shareIntent.setType("image/*"); 
       // Launch sharing dialog for image 
       mContext.startActivity(Intent.createChooser(shareIntent, "Share Image")); 
      } else { 
       Log.i("test", "Sharing failed, handler error."); 
      } 
     } 
    }); 

    return convertView; 
} 

// Returns the URI path to the Bitmap displayed in specified ImageView 
public Uri getLocalBitmapUri(ImageView imageView) { 
    // Extract Bitmap from ImageView drawable 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 

    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     Log.i("test", "is null"); 
     return null; 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 
+0

你本來可以友好地先解釋你的代碼。您首先從圖像視圖中提取位圖並將其保存爲PNG文件。之後,你想分享該圖像文件。而且你沒有告訴你的代碼在哪裏出錯。 – greenapps

+0

Is Ion是否也將下載的圖像保存到文件? – greenapps

回答

0

添加這些四行到getLocalBitmapUri -function得到它的工作對我來說:

bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bmp); 
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
drawable.draw(canvas); 

這裏是用更新的完整功能代碼:

// Returns the URI path to the Bitmap displayed in specified ImageView 
public Uri getLocalBitmapUri(ImageView imageView) { 
    // Extract Bitmap from ImageView drawable 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 

    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bmp); 
     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
     drawable.draw(canvas); 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 
相關問題