2015-10-20 73 views
0

我嘗試使用意向共享方式來分享圖片到Facebook。然而,當我這樣做,應用程序崩潰,這是代碼:想要共享圖像使用意圖的Facebook死機應用

Bitmap bitmap; 
Intent shareIntent = new Intent(); 
//Declared Globally 

File out = new File(getFilesDir(), "newImage.jpg"); 

    bitmap = BitmapFactory.decodeFile(out.getAbsolutePath()); 
    ImageView im1 = (ImageView)findViewById(R.id.camOut); 
    im1.setImageBitmap(bitmap); 
    im1.setScaleType(ImageView.ScaleType.FIT_XY); 

public void shareIt(View view) 
{ 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType("image/jpeg"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, bitmap); 
    startActivity(Intent.createChooser(shareIntent, "Share image using")); 
} 

這是我在logcat中獲得:

> --------- beginning of crash 
10-20 19:14:51.404 26669-26669/obx.com.futurister E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: obx.com.futurister, PID: 26669 
    java.lang.IllegalStateException: Could not execute method for android:onClick 
      at android.view.View$DeclaredOnClickListener.onClick(View.java:4452) 
      at android.view.View.performClick(View.java:5198) 
      at android.view.View$PerformClick.run(View.java:21147) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invoke(Native Method) 
      at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
            at android.view.View.performClick(View.java:5198) 
            at android.view.View$PerformClick.run(View.java:21147) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.app.ActivityThread.main(ActivityThread.java:5417) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Caused by: java.lang.RuntimeException: Failure from system 
      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514) 
      at android.app.Activity.startActivityForResult(Activity.java:3917) 
      at android.app.Activity.startActivityForResult(Activity.java:3877) 
      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820) 
      at android.app.Activity.startActivity(Activity.java:4200) 
      at android.app.Activity.startActivity(Activity.java:4168) 
      at obx.com.futurister.CameraTake.shareIt(CameraTake.java:52) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
            at android.view.View.performClick(View.java:5198) 
            at android.view.View$PerformClick.run(View.java:21147) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.app.ActivityThread.main(ActivityThread.java:5417) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Caused by: android.os.TransactionTooLargeException: data parcel size 4915988 bytes 
      at android.os.BinderProxy.transactNative(Native Method) 
      at android.os.BinderProxy.transact(Binder.java:503) 
      at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2657) 
      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507) 
            at android.app.Activity.startActivityForResult(Activity.java:3917) 
            at android.app.Activity.startActivityForResult(Activity.java:3877) 
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820) 
            at android.app.Activity.startActivity(Activity.java:4200) 
            at android.app.Activity.startActivity(Activity.java:4168) 
            at obx.com.futurister.CameraTake.shareIt(CameraTake.java:52) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
            at android.view.View.performClick(View.java:5198) 
            at android.view.View$PerformClick.run(View.java:21147) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.app.ActivityThread.main(ActivityThread.java:5417) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我猜的錯誤是在這一行:

shareIntent.putExtra(Intent.EXTRA_STREAM, bitmap); 

什麼來解決它的最好方法?

回答

1

則需要在該行

shareIntent.putExtra(Intent.EXTRA_STREAM, <instead pass imageuri>); 

通過圖像URI,而不是直接的位圖的你可以這樣做像以下:

File out = new File(getFilesDir(), "newImage.jpg"); 
    Uri uri = Uri.fromFile(out) 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 

看看它是否工作!

+0

謝謝,但再次試圖共享時,在WhatsApp的和Facebook的失敗,任何想法,爲什麼呢? :/ – OBX

+0

拋出的錯誤是什麼?與之前投擲的一樣? – Droidwala

+0

沒有錯誤,打開Facebook和WhatsApp的,那一切都很好,但是當點擊分享,無力,不知道如果我需要添加一些其他的權限? :/ – OBX