从url到facebook,whatsapp分享图片

时间:2017-03-09 12:51:34

标签: android

用于共享的旧代码不再适用于上面的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

<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" />

1 个答案:

答案 0 :(得分:2)

尝试{

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