我在Facebook应用程序中集成了Facebook API。我的主要应用目的是从相机拍摄图像并存储在SD卡中。通过Facebook API的集成,我在用户的墙上发布照片并进行适当的身份验证。问题,我所面对的是非常戏剧性的。有时我能够成功地在用户的墙上发布照片,有时我的应用程序已停止通知“不幸的是XXXXXXXX已停止工作”。失败率是1/5。请尽快帮助我 代码在这里
public class FbshareScreen extends Activity implements OnClickListener{
public Button _share;
private Button _decline;
int check;
public static final String APP_ID ="433644253229338";
private static final String[] PERMISSIONS = new String[] {"publish_stream"};
private com.android.fb.Facebook facebook;
private String path = ProjectUtil.createSdCardDirectory("/Camera/Photo");
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.facebookshare);
facebook = new Facebook(APP_ID);
_decline = (Button) findViewById(R.id.Button02);
_share = (Button)findViewById(R.id.Button01);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(path+"/image.png",options);
ImageView storedphoto= (ImageView)findViewById(R.id.storedphoto);
storedphoto.setImageBitmap(bitmap);
_decline.setOnClickListener(this);
_share.setOnClickListener(this);
facebook.setAccessExpires(5000);
}
private void logoutFromFacebook()
{
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.logout(FbshareScreen.this, new RequestListener()
{
@Override
public void onComplete(String response, Object state)
{
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true)
{
// User successfully Logged out
}
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
public void loginAndPostToWall()
{
facebook.authorize(this,PERMISSIONS,Facebook.FORCE_DIALOG_AUTH,new LoginDialogListener());
//facebook.authorize(this, new String[] {"publish_stream"}, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener() {});
}
String response;
public void postToWall(String message)
{
try {
addVango(BitmapFactory.decodeResource(getResources(), R.drawable.imagepicker),BitmapFactory.decodeFile(ProjectUtil.createSdCardDirectory("/Camera/Photo")+"/image.png"));
byte[] data = null;
try
{
FileInputStream fis = new FileInputStream(ProjectUtil.createSdCardDirectory("/Camera/Photo")+"/image.png");
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 10, baos);
data = baos.toByteArray();
}catch (Exception e)
{
Toast.makeText(FbshareScreen.this,e.toString(),Toast.LENGTH_SHORT).show();
}
response=facebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", parameters, "POST", new mRequestListener(), null);
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") || response.equals("false"))
{
Log.v("Error", "Blank response");
Toast.makeText(FbshareScreen.this,"Blank response",Toast.LENGTH_SHORT).show();
check=1;
}
else
{
check=2;
Intent FBshare_Home = new Intent(FbshareScreen.this,FacebookFeedback.class);
startActivity(FBshare_Home);
File file= new File(path+"/image.png");
file.delete();
finish();
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
class LoginDialogListener implements com.android.fb.Facebook.DialogListener
{
public void onComplete(Bundle values) {
new MailSendingTask().execute((Void[])null);
//postToWall("");
}
public void onFacebookError(FacebookError error)
{
showToast("Authentication with Facebook failed!");
File f= new File("/mnt/sdcard/Camera/Photo/image.png");
if(f.canRead())
{
f.delete();
}
Intent in=new Intent (FbshareScreen.this, HomeScreen.class);
startActivity(in);
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
File f= new File("/mnt/sdcard/Camera/Photo/image.png");
if(f.canRead())
{
f.delete();
}
Intent in=new Intent (FbshareScreen.this, HomeScreen.class);
startActivity(in);
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
File f= new File("/mnt/sdcard/Camera/Photo/image.png");
if(f.canRead())
{
f.delete();
}
Intent in=new Intent (FbshareScreen.this, HomeScreen.class);
startActivity(in);
finish();
}
}
class WallPostDialogListener implements com.android.fb.Facebook.DialogListener {
public void onComplete(Bundle values) {
logoutFromFacebook();
if (response != null)
{
showToast("Message posted to your facebook wall!");
} else {
showToast("Wall post cancelled!");
}
finish();
}
public void onFacebookError(FacebookError e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
public void onError(DialogError e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
public void onCancel() {
showToast("Wall post cancelled!");
finish();
}
}
private void showToast(String message)
{
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
public class mRequestListener implements RequestListener{
private static final String TAG = "I am Tag";
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onMalformedURLException *******************");
}
@Override
public void onIOException(IOException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onIOException *******************");
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onFileNotFoundException *******************");
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onFacebookError *******************");
}
@Override
public void onComplete(String response, Object state) {
Log.d(TAG, "******************* FACEBOOK::onComplete *******************");
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public void onClick(View view)
{
if(view == _share)
{
if(!ProjectUtil.haveNetworkConnection(FbshareScreen.this)){
Toast.makeText(FbshareScreen.this,"Unable to connect at this time please check your network connection and try again.",Toast.LENGTH_LONG).show();
return;
}
new FacebookSendingTask().execute((Void[])null);
loginAndPostToWall();
}else if(view == _decline){
File file= new File(path+"/image.png");
file.delete();
//Toast.makeText(getApplicationContext(), "Photo Deleted \nClick Another Photo", Toast.LENGTH_SHORT).show();
finish();
}
}
public Bitmap addVango(Bitmap c, Bitmap s)
{
Bitmap cs = null;
int width, height = 0;
if(c.getWidth() > s.getWidth())
{
width = c.getWidth();
height = c.getHeight() + s.getHeight();
} else {
width = s.getWidth();
height = c.getHeight() + s.getHeight();
}
cs = Bitmap.createBitmap(c.getWidth(), c.getHeight(), Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, c.getWidth()-s.getWidth()-75, c.getHeight()-s.getHeight()-155, null);
OutputStream os = null;
try {
os = new FileOutputStream(ProjectUtil.createSdCardDirectory("/Camera/Photo")+"/image.png");
cs.compress(CompressFormat.PNG, 100, os);
} catch(IOException e)
{
Log.e("combineImages", "problem combining images", e);
Toast.makeText(FbshareScreen.this,""+e,Toast.LENGTH_SHORT).show();
}
return cs;
}
public class MailSendingTask extends AsyncTask <Void, Void, Void> {
private ProgressDialog progressDialog;
public Looper mLooper;
@Override
protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(
FbshareScreen.this,
"Doing Processing", // title
"Posting Photo", // message
true // indeterminate
);
}
@Override
protected Void doInBackground(Void... params) {
//if(haveNetworkConnection())
try
{
postToWall("");
}
catch (Exception exception)
{
Log.e("Exception", exception.toString());
check=1;
}
return null;
}
@Override
protected void onPostExecute(Void v) {
this.progressDialog.cancel();
if(check==1)
Toast.makeText(getApplicationContext(), "Wall Post Cancelled", Toast.LENGTH_SHORT).show();
if(check==2)
Toast.makeText(getApplicationContext(), "Message Posted To your Wall", Toast.LENGTH_SHORT).show();
}
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
File file= new File(path+"/image.png");
if(file.canRead())
{
file.delete();
Intent in=new Intent(FbshareScreen.this,FacebookFeedback.class);
in.putExtra("value", "1");
startActivity(in);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public class FacebookSendingTask extends AsyncTask <Void, Void, Void> {
private ProgressDialog progressDialog;
public Looper mLooper;
@Override
protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(
FbshareScreen.this,
"Connecting To Facebook", // title
"Wait for a Moment", // message
true
);
}
@Override
protected Void doInBackground(Void... params) {
try {
facebook.logout(getApplicationContext());
} catch (Exception e) {
Log.e("Error", e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void v) {
this.progressDialog.cancel();
}
}
}
答案 0 :(得分:1)
您的问题出现是因为您的应用程序超出了Android设备的缓存限制。安卓设备的堆大小导致此内存异常。请升级内存或使用更高端的设备。
答案 1 :(得分:1)
我认为你在发布照片时会得到outofmemoryerror
。在运行时处理Android中图像的分配/释放是非常棘手的,因为它需要太多的堆。可能因为您的设备而遇到问题..我建议您升级内存或使用更高端的设备