Search code examples
javaandroidandroid-fragmentsuniversal-image-loader

Get imageview from Adapter in Fragment


I have Image view list in adapter on click of image I show full screen image. Where I have one button outside of adapter in fragment.

Now I want to get that image in fragment onClick of button to share that image. Below code this in adapter where my images are getting download.

I have using Android-Universal-Image-Loader library to show images.

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
    if(callback != null) {
        Bitmap bitmap = ((BitmapDrawable) img_main_bg.getDrawable()).getBitmap();
        callback.onItemClicked(bitmap);
    }
    spinner.setVisibility(View.GONE);
}

How do I get image in fragment?

I already tried interface but onLoadingComplete downloading multiple image at time so I can't get right image on that.


Solution

  • you can do it following way

    first make click listener in that ask for permission to save image

    imageView.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        imgView = imageView;
                        boolean hasPermission = (ContextCompat.checkSelfPermission(ImagePagerActivity.this,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
                        if (!hasPermission) {
                            ActivityCompat.requestPermissions(ImagePagerActivity.this,
                                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                    112);
                        }else
                        {
                            showDialog();
                        }
    
    
                    }
                });
    

    if permission is applied then save image first then share it

    private void showDialog()
        {
            new AlertDialog.Builder(ImagePagerActivity.this,R.style.MyAlertDialogStyle)
                    .setTitle("Select your option")
                    .setPositiveButton("Save",
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialog,
                                        int which) {
                                    imgView.buildDrawingCache();
                                    Bitmap bm = imgView
                                            .getDrawingCache();
                                    OutputStream fOut = null;
                                    try {
                                        File root = new File(
                                                Environment
                                                        .getExternalStorageDirectory()
                                                        + File.separator
                                                        + "Beauty"
                                                        + File.separator);
                                        if (!root.exists())
                                            root.mkdirs();
                                        File sdImageMainDirectory = new File(
                                                root,
                                                System.currentTimeMillis()
                                                        + ".jpg");
                                        fOut = new FileOutputStream(
                                                sdImageMainDirectory);
                                        bm.compress(
                                                Bitmap.CompressFormat.PNG,
                                                100, fOut);
                                        fOut.flush();
                                        fOut.close();
                                        Toast.makeText(
                                                ImagePagerActivity.this,
                                                "File saved at  Beauty  folder",
                                                Toast.LENGTH_SHORT)
                                                .show();
                                    } catch (Exception e) {
                                        Toast.makeText(
                                                ImagePagerActivity.this,
                                                "Error occured. Please try again later.",
                                                Toast.LENGTH_SHORT)
                                                .show();
                                        e.printStackTrace();
                                    }
                                }
                            })
                    .setNegativeButton("Share",
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialog,
                                        int which) {
                                    try {
                                        imgView.buildDrawingCache();
                                        Bitmap bm = imgView
                                                .getDrawingCache();
                                        OutputStream fOut = null;
    
                                        File root = new File(
                                                Environment
                                                        .getExternalStorageDirectory()
                                                        + File.separator
                                                        + " Beauty"
                                                        + File.separator);
                                        if (!root.exists())
                                            root.mkdirs();
                                        File sdImageMainDirectory = new File(
                                                root, "1.jpg");
                                        fOut = new FileOutputStream(
                                                sdImageMainDirectory);
                                        bm.compress(
                                                Bitmap.CompressFormat.PNG,
                                                100, fOut);
                                        fOut.flush();
                                        fOut.close();
    
                                        Intent shareIntent = new Intent(
                                                Intent.ACTION_SEND);
                                        Uri phototUri = Uri
                                                .fromFile(sdImageMainDirectory);
                                        shareIntent.setData(phototUri);
                                        shareIntent
                                                .setType("image/png");
                                        shareIntent.putExtra(
                                                Intent.EXTRA_STREAM,
                                                phototUri);
                                        startActivityForResult(Intent
                                                        .createChooser(
                                                                shareIntent,
                                                                "share using"),
                                                2);
    
                                    } catch (Exception ce) {
                                        ce.printStackTrace();
                                    }
    
                                }
                            })
    
                    .show();
        }