我正在尝试创建一个应用程序,允许从图库和Facebook共享图像和视频。我的编码中出现错误(以红色突出显示):" onActivityResult"," getContentResolver"," PlusShare"," setType&# 34;," 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>
有人可以指导我如何解决这个问题吗?任何建议都非常感谢。谢谢。
答案 0 :(得分:1)
试试这段代码,希望有所帮助。
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"));
答案 1 :(得分:1)
首先,您需要像这样修改Manifest.xml
文件
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
然后修改您的方法,如:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE) {
if (resultCode == RESULT_OK) {
taken = true;
getContentResolver().notifyChange(uri, null);
ContentResolver cr = getContentResolver();
try {
bmp = MediaStore.Images.Media.getBitmap(cr, uri);
imagePhoto.setImageBitmap(bmp);
Toast.makeText(UserProfile.this, "Image has been saved to " + file.getAbsolutePath() + " as " + file.getName(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
} else if (resultCode == RESULT_CANCELED) {
taken = false;
Toast.makeText(UserProfile.this, "Action canceled", Toast.LENGTH_SHORT).show();
}
}
}
如果你打算使用它的意图,请输入:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_CODE);
我使用这些值来使其工作
private static final int REQ_CODE = 1152;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
希望它有所帮助!!!
答案 2 :(得分:0)
您引用{}
的地方错误,您的onCreate
方法
匿名班View.OnClickListener
未正确关闭}
。
以下是正确放置报价的固定版本。
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);
}
}
}
}