2013-02-11 54 views
3

HI朋友任何機構可以幫助我..的Android上傳圖庫圖片至Facebook ------位圖圖像的內存不足

我要上傳我的畫廊圖片到Facebook和我使用此代碼。我內存不足例外。請給出完整的成功代碼,而不是這個。

這裏是我的代碼:

 UploadImage.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) 
         { 
         byte[] data = null; 
         Bitmap bi = BitmapFactory.decodeFile(path); 
         //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);    
         ByteArrayOutputStream baos = new ByteArrayOutputStream();    
         bi.compress(CompressFormat.JPEG, 100, baos); 
         data = baos.toByteArray(); 
         Bundle params = new Bundle(); 


         params.putString(Facebook.TOKEN, Login.mFacebook.getAccessToken());    
         params.putString("method", "photos.upload");    
         params.putByteArray("picture", data); 

         AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(Login.mFacebook);    
         mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener()); 
         Toast.makeText(getApplicationContext(), "Image Posted on Facebook.", Toast.LENGTH_SHORT).show(); 
         } 

       }); 
      } 

     } 

     class SampleUploadListener extends BaseKeyListener implements RequestListener { 

     public void onComplete(final String response, final Object state) { 
      try { 
       // process the response here: (executed in background thread) 
       Log.d("Facebook-Example", "Response: " + response.toString()); 
       JSONObject json = Util.parseJson(response); 
       final String src = json.getString("src"); 

       // then post the processed result back to the UI thread 
       // if we do not do this, an runtime exception will be generated 
       // e.g. "CalledFromWrongThreadException: Only the original 
       // thread that created a view hierarchy can touch its views." 

      } catch (JSONException e) { 
       Log.w("Facebook-Example", "JSON Error in response"); 
      } catch (FacebookError e) { 
       Log.w("Facebook-Example", "Facebook Error: " + e.getMessage()); 
      } 
     } 

     public int getInputType() { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     public void onComplete(String response) { 
      // TODO Auto-generated method stub 

     } 

     public void onIOException(IOException e) { 
      // TODO Auto-generated method stub 

     } 

     public void onFileNotFoundException(FileNotFoundException e) { 
      // TODO Auto-generated method stub 

     } 

     public void onMalformedURLException(MalformedURLException e) { 
      // TODO Auto-generated method stub 

     } 

     public void onFacebookError(FacebookError e) { 
      // TODO Auto-generated method stub 

     } 
} 
02-12 10:29:16.514: E/AndroidRuntime(9732): FATAL EXCEPTION: main 
     02-12 10:29:16.514: E/AndroidRuntime(9732): java.lang.OutOfMemoryError 
     02-12 10:29:16.514: E/AndroidRuntime(9732): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at com.kmiller.facebookintegration.GalleryImage$1.onClick(GalleryImage.java:83) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.view.View.performClick(View.java:3627) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.view.View$PerformClick.run(View.java:14329) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.os.Handler.handleCallback(Handler.java:605) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.os.Handler.dispatchMessage(Handler.java:92) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.os.Looper.loop(Looper.java:137) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at android.app.ActivityThread.main(ActivityThread.java:4511) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at java.lang.reflect.Method.invokeNative(Native Method) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at java.lang.reflect.Method.invoke(Method.java:511) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753) 
    02-12 10:29:16.514: E/AndroidRuntime(9732): at dalvik.system.NativeStart.main(Native Method) 
+0

請您分享日誌 – onkar 2013-02-11 13:15:58

回答

0

您需要修改代碼: UploadImage.setOnClickListener(新OnClickListener(){

 public void onClick(View v) 
        { 
        byte[] data = null; 
        Bitmap bi = BitmapFactory.decodeFile(path); 
        //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();    
        bi.compress(CompressFormat.JPEG, 100, baos); 
        bi.recycle(); 
        data = baos.toByteArray(); 
        Bundle params = new Bundle(); 


        params.putString(Facebook.TOKEN, Login.mFacebook.getAccessToken());    
        params.putString("method", "photos.upload");    
        params.putByteArray("picture", data); 

        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(Login.mFacebook);    
        mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener()); 
        Toast.makeText(getApplicationContext(), "Image Posted on Facebook.", Toast.LENGTH_SHORT).show(); 
        } 

      }); 
     }