2012-11-29 82 views
0

我在Android應用程序中集成了Facebook API。我的主要應用程序的目的是從相機拍攝圖像並存儲在SD卡中。通過Facebook API的整合,我將照片貼在用戶的牆上並進行了適當的驗證。問題是,我面對的是非常戲劇化的。有時我能夠成功地在用戶的牆上發佈照片,有時我的應用程序已停止通知「不幸XXXXXXXX已停止工作」。失敗的比例是1/5。請幫我儘快 代碼是在這裏使用Facebook的API集成在牆上發佈照片的問題

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(); 
    } 

    } 

}

回答

1

您的問題是出現因爲你的應用程序是超出你的Android device.The堆大小的高速緩存限制的Android設備造成這種內存異常。請升級內存或使用更高端的設備。

1

我以爲你在發佈照片時得到了outofmemoryerror。在運行時處理Android中圖像的分配/釋放是非常棘手的,因爲它需要太多的堆。可能你是因爲你的設備問題..我建議你請升級內存或使用更高端的設備

+1

失敗日誌 和>>> E/AndroidRuntime(2000):引起:java.lang。 EO/AndroidRuntime(2000):在android.graphics.Bitmap.nativeCreate(本地方法) android.graphics.Bitmap.createBitmap(Bitmap.java:551)E/AndroidRuntime(2000):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)E/AndroidRuntime(2000):在android.graphics.BitmapFactory。 finishDecode(BitmapFactory.java:524)E/AndroidRuntime(2000):at com.vangocam.FbshareScreen.postToWall(FbshareScreen.java:123) - – Ugrasen

相關問題