photo share intent in android

◇◆丶佛笑我妖孽 提交于 2019-12-29 01:51:06

问题


Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");

Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;

i was used above code for sharing image on social sites.when i posting image on facebook only text is posted and image is not coming.how can we get the image and pathtoimage is string variable i am getting sever image path and stored in string variable.but it is not working.please give any solutions for my above question.?


回答1:


Try this

Bitmap icon = mBitmap;
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {                       
            e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
    startActivity(Intent.createChooser(share, "Share Image"));

Refer more links

http://android-developers.blogspot.in/2012/02/share-with-intents.html

Android Share Via Dialog

Image post into facebook wall from android sdcard




回答2:


This is the solution I came up with: This function allows you to share to instagram, facebook or any other app if you know the package name. If the app is not installed it redirects to the Play Store. If you send "others" as parameters, you will display a list of apps that also support image sharing.

shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"

 public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
        if ((intent != null)||(app.equals("others"))){

            // Create the new Intent using the 'Send' action.
            Intent share = new Intent(Intent.ACTION_SEND);

            if(!app.equals("others"))
                share.setPackage(app);

            // Set the MIME type
            share.setType(type);    

            // Create the URI from the media
            File media = new File(mediaPath);
            Uri uri = Uri.fromFile(media);

            // Add the URI and the caption to the Intent.
            share.putExtra(Intent.EXTRA_STREAM, uri);
            share.putExtra(Intent.EXTRA_TEXT, caption);
            // Broadcast the Intent.
            //startActivity(Intent.createChooser(share, "Share to"));
            a.startActivity(share);
            }
        else{
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
            intent.setData(Uri.parse("market://details?id="+app));
            a.startActivity(intent);            
            }   
        }



回答3:


You can not use Intent to post to Facebook. They have specifically blocked this. You have to either use their SDK or copy it to the clipboard and have the user paste it after the facebook interface opens. I am having this same problem.



来源:https://stackoverflow.com/questions/19218243/photo-share-intent-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!