我试图创建一个应用程序,以允许将图像和视频从画廊共享到Facebook。我的代码在这行中弹出错误(突出显示为红色):“ onActivityResult”,“ getContentResolver”,“ PlusShare”,“ setType”,“ addStream”,“ setText”和“ getIntent”表示无法解析方法。完整编码如下:

MainActivity.java

package com.example.dothis.facebook;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private static final int REQ_SELECT_PHOTO = 1;
private static final int REQ_START_SHARE = 2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setResult(RESULT_OK);

    // listeners of our two buttons
    View.OnClickListener handler = new View.OnClickListener() {
        public void onClick(View v) {


            Button shareMediaButton = (Button)    findViewById(R.id.share_media);
            shareMediaButton.setOnClickListener(new   android.view.View.OnClickListener() {
                public void onClick(View v) {
                    Intent photoPicker = new     Intent(Intent.ACTION_PICK);
                    photoPicker.setType("video/*, image/*");
                    startActivityForResult(photoPicker, REQ_SELECT_PHOTO);
                }
            });

        }

        @Override
        protected void onActivityResult(int requestCode, int   resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);

            if (requestCode == REQ_SELECT_PHOTO) {
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = intent.getData();
                    ContentResolver cr = this.getContentResolver();
                    String mime = cr.getType(selectedImage);

                    PlusShare.Builder share = new  PlusShare.Builder(this);
                    share.setText("hello everyone!");
                    share.addStream(selectedImage);
                    share.setType(mime);
                    startActivityForResult(share.getIntent(),   REQ_START_SHARE);

                }
            }
        }

    }
   }


Android清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dothis.facebook" >

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />


    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"  />
        </intent-filter>
    </activity>
</application>

</manifest>


有人可以指导我如何解决这个问题吗?任何建议都将受到赞赏。谢谢。

最佳答案

试试此代码,希望对您有所帮助。

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/path/to/image.ext"));

    startActivity(Intent.createChooser(share, "Share Image"));

08-17 19:47