Android使用glide在whatsapp上使用viewPager的意图共享滑动图像

Android使用glide在whatsapp上使用viewPager的意图共享滑动图像,android,android-viewpager,Android,Android Viewpager,我正在开发一个在线画廊应用程序,在一个片段对话框中,将特定图片显示为一个viewPager。 但主要的问题是我无法在whatsapp上共享特定的图像,因为我发现很难获得imageViewPreview 下面是在viewPager中加载图像的代码 public class MyViewPagerAdapter extends PagerAdapter { private LayoutInflater layoutInflater; public MyViewPagerAdapt

我正在开发一个在线画廊应用程序,在一个片段对话框中,将特定图片显示为一个viewPager。 但主要的问题是我无法在whatsapp上共享特定的图像,因为我发现很难获得imageViewPreview

下面是在viewPager中加载图像的代码

public class MyViewPagerAdapter extends PagerAdapter {

    private LayoutInflater layoutInflater;

    public MyViewPagerAdapter() {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);

        final ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);

        Image image = images.get(position);

        Glide.with(getActivity()).load(image.getLarge())
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageViewPreview);

        container.addView(view);
返回视图; }

现在我正在尝试从imageViewPreview中获取图像

whatsappShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 Log.e(TAG, "Click working");
                //shareImage();
               // ImageView imageWhatsapp = (ImageView) view.findViewById(R.id.image_preview);
                Uri bmpUri = getLocalBitmapUri(imageViewPreview);
                if (bmpUri != null) {
                    // Construct a ShareIntent with link to image
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.setType("image/*");
                    shareIntent.setPackage("com.whatsapp");
                    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    // Launch sharing dialog for image
                    startActivity(Intent.createChooser(shareIntent, "Share Image"));
                } else {
                    // ...sharing failed, handle error
                    Log.e(TAG, "ERROR" + bmpUri);
                }
            }

        });
我的getLocalBitmapUri()方法如下:

public Uri getLocalBitmapUri(ImageView imageViewPreview) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageViewPreview.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageViewPreview.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);

        Log.e(TAG, "popopo: " + file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

But the problem is I am getting **NULL** from getLocalBitmapUri() method.
please guide me the get imageViewPreview.

我曾经面对过这种方法的亵渎,几天前我使用过它并开始工作,我忘了是谁给出了这个片段

public Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

Image=images.get(位置)图像是来自存储还是来自资源/绘图?如果无法获取Uri,请尝试缓存ImageView并将其转换为位图。这里的代码片段我使用了来自JSON API的图像。试试这个谢谢Agi。我将跟随你给定的链接。将尽快通知你。上述代码是不工作的,因为我需要得到图像后,从上述代码加载图像查看。
public Uri getLocalBitmapUri(ImageView imageViewPreview) {
   // Extract Bitmap from ImageView drawable
   Drawable drawable = imageViewPreview.getDrawable();
   Bitmap bmp = drawableToBitmap(imageViewPreview);
   //...................
   try {
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
    file.getParentFile().mkdirs();
    if(!file.exist()) file.createNewFile();
    //..............