I built a recyclerview app. Now, when an item list is clicked it takes you to DetailActivity. In this DetailActivity i have the full image. this image is what i want a user to share with any available installed app using Intent. But all my effort with experience i had in the past is not working.
The issue here is that I received the image in drawable from Adapter with this
int imageResourceId = intent.getIntExtra("iImage", -1);
imageView.setImageResource(imageResourceId);
And I am trying to share this image on Button ClickListener with intent. How do i recall this imageResourceId
or this "iImage"
in order to identify the exact image i want to share.
This is the DetailActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
mBrandNewDesc = findViewById(R.id.textView);
Button ImageShareButton = (Button)findViewById(R.id.btnImageShare);
ImageShareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageView);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(shareIntent);
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
});
//get data from previous activity when item of activity is clicked using intent
Intent intent = getIntent();
String mActionBarTitle = intent.getStringExtra("actionBarTitle");
String newDescription = intent.getStringExtra("brandNewDesc");
imageView = findViewById(R.id.ImageFoot1);
if (intent != null && !intent.getExtras().isEmpty()) {
int imageResourceId = intent.getIntExtra("iImage", -1);
imageView.setImageResource(imageResourceId);
}
//setctionBar Title
actionBar.setTitle(mActionBarTitle);
//get text in text textView
mBrandNewDesc.setText(Html.fromHtml(newDescription));
//ok we are done, lets run the project
}
public void ShareItem(View view) {
//share Copied Text characters via other apps button
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, mBrandNewDesc.getText().toString());
sendIntent.setType("text/plain");
Intent.createChooser(sendIntent, "Share via");
startActivity(sendIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.page_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is MyAdapter class
public class MyAdapter extends RecyclerView.Adapter<MyHolder> implements Filterable {
Context mContext;
ArrayList<Model> models, filterList; // this array list create a list of array which parameter define in our class
CustomFilter filter;
public MyAdapter(Context context, ArrayList<Model> models) {
this.mContext = context;
this.models = models;
this.filterList = models;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row, null); //this line inflate our row
return new MyHolder(view); //this will return our view to holder class
}
@Override
public void onBindViewHolder(@NonNull final MyHolder myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); //here is position
myHolder.mDesc.setText(models.get(i).getDesc());
myHolder.mImageView.setImageResource(models.get(i).getIcon());
myHolder.mSubImageView.setImageResource(models.get(i).getIcon2());// here we used imge resource
myHolder.setItemCLickListener(new ItemClickListener() {
@Override
public void onItemClickListener(View v, int position) {
String gTitle = models.get(position).getTitle();
String gDesc = models.get(position).getDesc();
int imageId = models.get(position).getIcon();
//get our data with intent
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("actionBarTitle", models.get(position).getTitle());
intent.putExtra("brandNewDesc", models.get(position).getBrandNewDesc());
intent.putExtra("iImage", imageId);
mContext.startActivity(intent);
return;
}
});
}
@Override
public int getItemCount() {
return models.size();
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new CustomFilter(filterList, this);
}
return filter;
}
}