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.
'圖片圖像= images.get(位置);'是從存儲或從資源/繪製圖像?如果它不可能沒有獲得Uri,請嘗試獲取ImageView緩存並將其轉換爲Bitmap。這裏的代碼片段http://stackoverflow.com/a/17288102/2741453 –
我已經使用JSON API的圖片。 –
試試這個http://stackoverflow.com/a/42200812/2741453 –