我试图在recycleview android中分享并保存来自cardview的imageview。 我的imagview是一个静态文件,我想在android库中保存这个文件,当我点击共享图像按钮时也分享这个图像。 请看下面的快照!
我的代码如下:
public class PicCardViewHolder extends RecyclerView.ViewHolder {
public TextView pic_name;
public ImageView st_image;
public ImageView save_img;
public ImageView share_st_img;
public Drawable drawable;
public Bitmap bitmap;
public String ImagePath;
public Uri URI;
public Context context;
public PicCardViewHolder(View v) {
super(v);
context = itemView.getContext();
pic_name = (TextView) itemView.findViewById(R.id.pic_name);
st_image = (ImageView) itemView.findViewById(R.id.st_pic);
save_img = (ImageView) itemView.findViewById(R.id.down_img);
share_st_img = (ImageView) itemView.findViewById(R.id.share_pic_st);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == 0) {
//code for save image into gallery android
st_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawable = ContextCompat.getDrawable(context, R.drawable.test_image);
bitmap = ((BitmapDrawable) drawable).getBitmap();
ImagePath = MediaStore.Images.Media.insertImage(
context.getContentResolver(),
bitmap,
"demo_image",
"demo_image"
);
URI = Uri.parse(ImagePath);
Toast.makeText(context, "Image Saved Successfully", Toast.LENGTH_LONG).show();
}
});
}
}
});
}
}
答案 0 :(得分:0)
我解决了我的问题:)
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == 0) {
share_st_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//
drawable = ContextCompat.getDrawable(context, R.drawable.test_image);
bitmap = ((BitmapDrawable) drawable).getBitmap();
ImagePath = MediaStore.Images.Media.insertImage(
context.getContentResolver(),
bitmap,
"demo_image",
"demo_image"
);
URI = Uri.parse(ImagePath);
Toast.makeText(context, "Image GET Successfully", Toast.LENGTH_LONG).show();
//share image code
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri photoUri = Uri.parse(ImagePath);
shareIntent.setData(photoUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
context.startActivity(Intent.createChooser(shareIntent, "Use this for sharing"));
}
});
save_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawable = ContextCompat.getDrawable(context, R.drawable.test_image);
bitmap = ((BitmapDrawable) drawable).getBitmap();
ImagePath = MediaStore.Images.Media.insertImage(
context.getContentResolver(),
bitmap,
"demo_image",
"demo_image"
);
URI = Uri.parse(ImagePath);
Toast.makeText(context, "Saved Successfully to Gallery", Toast.LENGTH_LONG).show();
// addImageToGallery(context, ImagePath, "he", "hi");
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/StatusQuotes Images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(fname, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
});
}