2017-03-31 28 views
0

我正在使用在線圖庫應用程序,在一個片段中作爲viewPager從特定的顯示對話框圖像。 但主要問題我無法分享whatsapp上的特定圖像,因爲我發現難以獲得imageViewPreview。Android在使用滑動的viewPager上使用意圖在whatsapp上分享滑動圖像

以下是在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); 

返回視圖用於加載圖像的代碼; }

現在我想從

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()methos是遵循imageViewPreview獲得的圖像:

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. 
+0

'圖片圖像= images.get(位置);'是從存儲或從資源/繪製圖像?如果它不可能沒有獲得Uri,請嘗試獲取ImageView緩存並將其轉換爲Bitmap。這裏的代碼片段http://stackoverflow.com/a/17288102/2741453 –

+0

我已經使用JSON API的圖片。 –

+0

試試這個http://stackoverflow.com/a/42200812/2741453 –

回答

0

我曾面臨sacreate這種方法,一些前段時間我使用它和工作,我忘了誰給這個片段。

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

在您的getLocalBitmapUri()

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(); 
    //..............