如何在自定义列表视图中共享外部图像?

时间:2015-05-05 15:53:00

标签: android listview

我创建了一个由自定义适配器处理的自定义列表视图。此列表中的每个项目都显示图像和共享按钮。使用离子从外部源加载图像。这很好。

现在我想在用户点击按钮时分享图片。虽然我能够共享文本等,但即使在实现此代码后,我也无法共享这些外部图像: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;
}

1 个答案:

答案 0 :(得分:0)

将这四行添加到getLocalBitmapUri - 函数让它对我有用:

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