从片段中的适配器获取图像视图

Get imageview from Adapter in Fragment

我在适配器中有图像视图列表,点击图像我显示全屏图像。我在片段中的适配器外部有一个按钮。

现在我想在点击按钮的片段中获取该图像以共享该图像。 下面在我的图像正在下载的适配器中编写代码。

我使用 Android-Universal-Image-Loader 库来显示图像。

@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);
}

如何获取片段中的图像?

我已经尝试过界面,但是 onLoadingComplete 一次下载多个图像,所以我无法在上面获得正确的图像。

您可以按照以下方式进行

首先在请求保存图像的权限中创建点击监听器

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();
                    }


                }
            });

如果已申请权限,则先保存图片再分享

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();
    }