Uploading Android Images to Facebook ------ Bitmap Image from Memory

Hello friends, any body can help me ..

I need to upload my gallery image to Facebook and I am using this code. I am out of memory Exception. Please give the complete successful code instead.

Here is my code:

       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)

      

+3


source to share


1 answer


You need to change your code like: 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);
                    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();
                    } 

          });
       } 

      

0


source







All Articles