我想分享并保存在recyclelerview中的imageview图像onbuttonclick在MyViewHolder中

时间:2017-01-16 19:44:38

标签: java android android-recyclerview

这是MyViewHolder.java 它说无法解决startActivity方法。

编译时,表示不支持文件格式。如何找到图像位置?它说无法解决getResource();方法

我想在ImageView

中的RecyclerView onButtonClick分享并保存MyViewHolder的图片
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyViewHolder extends RecyclerView.ViewHolder  {

 public TextView titleTextView;
 public ImageView coverImageView;
 public ImageView likeImageView;
 public ImageView shareImageView;
 public ImageView downloadImageView;

public MyViewHolder(View v) {
 super(v);
 titleTextView = (TextView) v.findViewById(R.id.titleTextView);
 coverImageView = (ImageView) v.findViewById(R.id.coverImageView);
 likeImageView = (ImageView) v.findViewById(R.id.likeImageView);
 shareImageView = (ImageView) v.findViewById(R.id.shareImageView);
 downloadImageView= (ImageView) v.findViewById(R.id.downloadImageView);   
//It is shareImageView in viewholder Method.
//onClick share the recycler image to another app
  shareImageView.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {

 Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
 "://" + getResources().getResourcePackageName(coverImageView.getId())
  + '/' + "drawable" + '/' + getResources().getResourceEntryName((int)coverImageView.getTag()));


Intent shareIntent = new Intent();
 Bitmap bitmap =viewToBitmap(coverImageView,coverImageView.getWidth(),coverImageView.getHeight());
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,imageUri);

shareIntent.setType("image/*");
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"coverImageView.jpg");
try {
file.createNewFile();
FileOutputStream fileOutputStream=new FileOutputStream(file);
                                fileOutputStream.write(byteArrayOutputStream.toByteArray());

} catch (IOException e) {
e.printStackTrace();
}

startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

 }
   });
}}

0 个答案:

没有答案