简体   繁体   English

从url共享图像到facebook,whatsapp

[英]Share image from url to facebook,whatsapp

Old codes for sharing is no longer working in API 23 above.i am loading the image in a TouchImageView from url and i want to share the pic to whatsapp ,facebook etc. I couldnt find any reliable new codes in internet and i tried few methods to share but all of them failed.Here is my code 用于共享的旧代码在上面的API 23中不再起作用。我正在从url中将图像加载到TouchImageView中,我想将图片共享到whatsapp,facebook等。我在互联网上找不到任何可靠的新代码,我尝试了几种方法分享,但都失败了。这是我的代码

public class GalleryActivityshare extends DialogFragment
 {

TouchImageView iv;
String url;

static GalleryActivityshare newInstance() {
    GalleryActivityshare f = new GalleryActivityshare();
    return f;
}

@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_galleryshare, container, false);


    url=getArguments().getString("url");

    iv = (TouchImageView) v.findViewById(R.id.imgDisplay);

    new ImageLoadTask(url,iv).execute();

    return v;

}
 public void share(Context context, Uri bmpUri) throws IOException {
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.id.imgDisplay);
        File file = new File(getActivity().getCacheDir(), String.valueOf(largeIcon
                + ".png"));
        FileOutputStream fOut = new FileOutputStream(file);
        largeIcon.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
        startActivity(intent);

    }


}

XML XML格式

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<com.touchlive.info.paramount.util.TouchImageView

    android:id="@+id/imgDisplay" 
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter" />

<ImageView
    android:tint="#123654"
    android:onClick="share"
    android:id="@+id/shared"
    android:src="@drawable/share"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_width="40dp"
    android:layout_height="40dp" />

try { 尝试{

                Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), Your-Image);
                File file = new File(PageView.this.getCacheDir(), String.valueOf(largeIcon
                        + ".png"));
                FileOutputStream fOut = new FileOutputStream(file);
                largeIcon.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
                file.setReadable(true, false);
                final Intent intent = new Intent(     Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                intent.setType("image/png");
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM