-1

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;
    }
}
Joseph
  • 400
  • 4
  • 19
  • okay let me be clear you want to share a picture which is in drawable folder and you want to share it with all available shareable apps Right? – Jyotish Biswas Aug 15 '20 at 10:12
  • Yes Sir. That is what i want – Joseph Aug 15 '20 at 10:15
  • If you had a normal file to share you would use a FileProvider to do so. But FileProvider cannot serve from resource. So implement your own ContentProvider for this. Or copy the picture from resource to file system and after that you can use FileProvider for the copy. – blackapps Aug 15 '20 at 10:23
  • Please I'm still learning can you show me example from my code. Thanks in anticipation – Joseph Aug 15 '20 at 10:25

1 Answers1

0

With This method you have to save the Image into storage then you can share

This method needs storage permision

public void shareDrawable(Context context,int resourceId,String fileName) {
   try {
    //convert drawable resource to bitmap
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);

    //save bitmap to app cache folder
    File outputFile = new File(context.getCacheDir(), fileName + ".png");
    FileOutputStream outPutStream = new FileOutputStream(outputFile);
    bitmap.compress(CompressFormat.PNG, 100, outPutStream);
    outPutStream.flush();
    outPutStream.close();
    outputFile.setReadable(true, false);

    //share file
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
    shareIntent.setType("image/png");
    context.startActivity(shareIntent);
    } 
     catch (Exception e) { Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
    }
}

Reference : Link

With this method no need to save the image

try {
    Uri imageUri = null;
    try {
        imageUri = 
    Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(),
                BitmapFactory.decodeResource(getResources(), 
    R.drawable.fragment_menu_logo), null, null));
    } catch (NullPointerException e) {
    }
    
     //share file
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("image/png");
    context.startActivity(shareIntent);
    } catch (android.content.ActivityNotFoundException ex) {
    
}

Reference : Link

Jyotish Biswas
  • 674
  • 5
  • 11
  • Appreciate your help sir, remember i formed an array of images in position as passed through adapter to DetailActivity.java, But your method is looking for an image directly from drawable like you pointed out fragment_menu_logo as example – Joseph Aug 15 '20 at 10:37
  • this is why i asked you in comment that you want to share a image from drawable folder or not? – Jyotish Biswas Aug 15 '20 at 10:41
  • tell me this things, what do you have? 1 is your image in drawable folder ?, 2 do you have image url ?, 3 do you want to share multiple image? – Jyotish Biswas Aug 15 '20 at 10:42
  • Sorry sir I didn't understand the question correctly. Please help me and edit with correct answer so that i can accept it as correct answer – Joseph Aug 15 '20 at 10:43
  • for that i need to know what you exactly need please explain – Jyotish Biswas Aug 15 '20 at 10:45
  • if you have the image id then you can `R.drawable.fragment_menu_logo` with your id or in `shareDrawable` method pas your resource id – Jyotish Biswas Aug 15 '20 at 10:53
  • My App is a recyclerview with many images in Drawable folder. So when i click a recyclerview item it then opens into DetailActivity through the hlpe of MyAdapter class. Now this image is received is what i want to share using intent and it is a single image. – Joseph Aug 15 '20 at 10:55
  • I made an array of images like this:: //Listview icons for song titles in position icon = new int[]{R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower, R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,R.drawable.flower,}; – Joseph Aug 15 '20 at 10:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219867/discussion-between-jyotish-biswas-and-joseph). – Jyotish Biswas Aug 15 '20 at 10:58
  • And then passed it through adapter so as to show it as single files each when clicked – Joseph Aug 15 '20 at 10:59