【问题标题】:Sharing Image and Text On twitter not working在 Twitter 上共享图像和文本不起作用
【发布时间】:2014-01-03 08:34:36
【问题描述】:

这是我在下面给出的代码。FrameLayout 用于在 twitter 上共享为 JPEG 图像。这是我正在尝试的代码。

sharesp=(Spinner)findViewById(R.id.spnrShare);
        sharesp.setAdapter(new MyShAdapter(MainActivity.this, R.layout.rowview, Sharestring));
        sharesp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                selectitem=parent.getItemAtPosition(position).toString();
                selecetint=position;

                if(selectitem=="FaceBook" || selecetint==R.drawable.fbbtn){
                       shareFbook();

                }
                else if(selectitem=="Twitter" || selecetint==R.drawable.twitbtn){
                    shareTwitter();

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


public void shareTwitter(){

         FrameLayout savFrame_layout=(FrameLayout)findViewById(R.id.frame);
        try {
            if(MainActivity.this.mainFrame_layout==null){
            MainActivity.this.mainFrame_layout.setDrawingCacheEnabled(true);
              MainActivity.this.mainFrame_layout.refreshDrawableState();
              MainActivity.this.mainFrame_layout.buildDrawingCache();

            MainActivity.this.bm_ImgFrame = mainFrame_layout.getDrawingCache();

        int i1=100000;
        Random random=new Random();
        i1=random.nextInt(i1);
           MainActivity.fname = "Quick_"+ i1 + ".jpg";

            String pathy=Environment.getExternalStorageDirectory()+File.separator+"/MYAnApps";
            MainActivity.this.rootFile=new File(pathy);

            MainActivity.this.sdImageMainDirectory = new File(MainActivity.this.rootFile + MainActivity.fname);
            FileOutputStream fileOutputStream = new FileOutputStream(MainActivity.this.sdImageMainDirectory);
            bm_ImgFrame.compress(CompressFormat.JPEG, 80, fileOutputStream);

            Uri uri = Uri.parse(MainActivity.this.sdImageMainDirectory.getAbsolutePath());

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("/*");
                intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
                intent.putExtra(Intent.EXTRA_TEXT, "MyAndroidApp: https://play.google.com/store/apps/details?id=com.example.MyApp");
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(MainActivity.this.sdImageMainDirectory));
                startActivity(intent);            
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

    }

【问题讨论】:

    标签: android android-intent android-spinner android-twitter


    【解决方案1】:

    尝试使用"*/*" 作为 MIME,这样您就可以发送任何通用数据类型。

     intent.setType("*/*");
    

    还可以尝试将ACTION_SEND 更改为专门用于传递多个数据的ACTION_SEND_MULTIPLE

    有关ACTION_SEND_MULTPLE 和处理 MIME 类型的更多信息。

    【讨论】:

      【解决方案2】:

      检查这个例子这对我有用

      Twitter Image And text sharing Example

      用这个分享推特

        private void showTwittDialog() {
      
          final Dialog dialog = new Dialog(getApplicationContext());
          dialog.setContentView(R.layout.twitt_dialog);
          dialog.setTitle("Twitter");
      
          Button btnPost = (Button) dialog.findViewById(R.id.btnpost);
          final EditText et = (EditText) dialog.findViewById(R.id.twittext);
          et.setText(sharingStr);
          et.setSelection(et.getText().length());
          btnPost.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                    twitt_msg = et.getText().toString().trim();   
                  if (twitt_msg.length() == 0) {
                      showToast("Tweet is empty!!!");
                      return;
                  } else if (twitt_msg.length() > 140) {
                      showToast("Tweet of more than 140 characters not allowed!!!");
                      return;
                  }
                  dialog.dismiss();
                  new PostTwittTask().execute(twitt_msg);
              }
          });
          dialog.show();
      }
      
      protected void Share_to_twitter() {  
          Intent tweetIntent = new Intent();
          tweetIntent.setType("text/plain");
          tweetIntent.putExtra(Intent.EXTRA_TEXT, sharingStr);
          final PackageManager packageManager =  getPackageManager();
          List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
      
          for (ResolveInfo resolveInfo : list) {
              twitter_dialog = resolveInfo.activityInfo.packageName;
          }
      
          if (twitter_dialog != null  && twitter_dialog.startsWith("com.twitter.android")) {
              // if the native twitter app is found
              tweetIntent.setPackage(twitter_dialog);
              startActivity(tweetIntent);
          } else {
              showTwittDialog();
          }
          mTwitter.resetAccessToken();        
      }
      
      class PostTwittTask extends AsyncTask<String, Void, String> {
          ProgressDialog pDialog;
      
          @Override
          protected void onPreExecute() {
              pDialog = new ProgressDialog(getApplicationContext());
              pDialog.setMessage("Posting Twitt...");
              pDialog.setCancelable(false);
              pDialog.show();
              super.onPreExecute();
          }
      
          @Override
          protected String doInBackground(String... twitt) {
              try {
                  mTwitter.updateStatus(twitt[0]);
                  return "success";
      
              } catch (Exception e) {
                  if (e.getMessage().toString().contains("duplicate")) {
                      return "Posting Failed because of Duplicate message...";
                  }
                  e.printStackTrace();
                  return "Posting Failed!!!";
              }
          }
      
          @Override
          protected void onPostExecute(String result) {
              pDialog.dismiss();
      
              if (null != result && result.equals("success")) {               
                  View popUpView = getLayoutInflater().inflate(R.layout.share_successfully_popup,null);   
                  final PopupWindow popUp = new PopupWindow(popUpView,UtilityClass.dynamicScalingForWidth(100),
                          UtilityClass.dynamicScalingForWidth(100),true);
                  popUp.showAtLocation(popUpView.findViewById(R.id.share_successfully_ctr),Gravity.CENTER,0, 0); 
      
                  Handler handler = new Handler();
                  Runnable r = new Runnable() {
                      public void run() {
                          popUp.dismiss();
                      }
                  };
                  handler.postDelayed(r, 3000);           
      
              } else {
                  showToast(result);
              }
              super.onPostExecute(result);
          }
      }
      

      【讨论】:

      • 我在 FrameLayOut 中有一个 ImageViews(Gallery Images)。我将 FrameLayout 压缩为 JPEG。现在我想在 Twitter 上分享这张图片。看看我的问题。
      猜你喜欢
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      相关资源
      最近更新 更多