不工作在Share Intent中共享图像在android中

时间:2014-03-19 06:58:32

标签: android facebook android-intent

我在android中使用了ShareIntent。我希望通过shareintent.my代码在facebook中分享位图图像。如何解决这个问题。

感谢!!!

public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle() == "Facebook") {


             Uri pngUri = Uri.parse("file//res/drawable/camera.png");
             Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
             shareIntent.setType("image/png");
             shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"YOUR TEXT HERE");
             shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"YOUR TEXT HERE");
             shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
             PackageManager pm =getActivity().getApplicationContext().getPackageManager();
             List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if ((app.activityInfo.name).contains("facebook")) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                                activity.applicationInfo.packageName,
                                activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    startActivity(shareIntent);
                        }
                    }
                }
        else if (item.getTitle() == "Twitter") {
            Toast.makeText(getActivity().getApplicationContext(), "Twitter",
                    1).show();
        } else {
            return false;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

试试这个

//在保存按钮上添加代码

     File file;
                     Bitmap bitmap=((BitmapDrawable)imageview.getDrawable()).getBitmap();
                     ByteArrayOutputStream stream1=new ByteArrayOutputStream();
                     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
                     byte[] imageInByte2=stream1.toByteArray();

                      try{
                     String  path = Environment.getExternalStorageDirectory().toString();
                     File  imgDirectory = new File(Environment.getExternalStorageDirectory()+"/Cards/");
                     imgDirectory.mkdirs();
                     OutputStream fOut = null;
                         file = null;
                     file = new File(path,"/Cards/"+etcardname.getText().toString()+ ".png");
                     Toast.makeText(getActivity(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                     fOut = new FileOutputStream(file);
                     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                     fOut.flush();
                     fOut.close();

                     }catch(Exception e){
                     e.printStackTrace();
                     }

            //share image code
             Intent share = new Intent(android.content.Intent.ACTION_SEND);
             share.setType("image/*");
             share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
             startActivity(Intent.createChooser(share, "Share image using"));