mardi 5 mai 2015

Can't share external image in custom listview with Ion or Picasso

I have created a custom listview that displays an image, a short text and a "share" button for each list item. The images are loaded from external sources with Ion (though the same problem exists when I use Picasso instead) and now I try to share the image when the user clicks on the "Share"-Button.

I can share simple text, though having trouble to share an external image. I tried implementing the following part but I always end up in the "else"-clause of the if (bmpUri != null)-statement.

I also added the following two lines in my AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Anybody has an idea why I'm not able to share these images? Might this be because I pass through the ImageView with the "set/getTag" to the onClick method?

Code of my CustomAdapter:

public class ContentAdapter extends BaseAdapter {

private Context mContext;
private Content[] mContents;

public ContentAdapter(Context context, Content[] contents){
    mContext = context;
    mContents = contents;
}

@Override
public int getCount() {
    return mContents.length;
}

@Override
public Object getItem(int position) {
    return mContents[position];
}

@Override
public long getItemId(int position) {
    return 0; //we aren't going to use this. Tag items for easy reference
}

@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())
            .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("sdf", "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 {
        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;
}

private static class ViewHolder {
    ImageView contentImageView; //public by default
    TextView contentLabel;
    Button contentShareButton;
}

}

Aucun commentaire:

Enregistrer un commentaire