Android: Exception with sending ParcelFileDescript

2019-01-28 18:44发布

问题:

I am writing an android app where my service needs to send an image to some other apps (via a broadcast message or by starting a service - there a multiple apps that might be interested to receive the image).

If I load the image into an Bitmap object and put it as an "Extra" of an Intent it would actually work. However, I want to see if I can send a ParcelFileDescrptor around instead, and let the client load the Bitmap object on their own (from reading the spec, it looks like ParcelFileDescriptor is created just for this purpose - sharing files between processes). Here I am trying to avoid sending large objects via Intent. So I wrote something like this:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    System.out.println("Service is called" + this.getClass());
    Intent newIntent = new Intent(MY_ACTION);
    try {
        File icon = new File(getExternalFilesDir(null), "robot_icon.jpg");
        icon.setReadable(true, false);
        if( !icon.exists() ) {
            System.out.println("Writting file " + icon);
                FileOutputStream out;
                out = new FileOutputStream(icon);
                BitmapFactory.decodeResource(getResources(), R.drawable.two_face_answer_map).compress(CompressFormat.JPEG, 100, out);
                out.close();
                System.out.println("Closing file after writing" + icon);
        }

        newIntent.putExtra(EXTRA_BITMAP, ParcelFileDescriptor.open(icon, ParcelFileDescriptor.MODE_READ_WRITE));
//          sendBroadcast(newIntent);
        startService(newIntent);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Error opening robot icon file", e);
    }catch (IOException e) {
        Log.e(TAG, "Error opening robot icon file", e);
    }

    System.out.println("No Exception");
    return super.onStartCommand(intent, flags, startId);
}

When this code is executed, I always receive a RuntimeException that said "Not allowed to write file descriptor here". Please note that I see the problem both with sendBroadcast as well as startService options. Anybody knows why it is not allowed here? What did I do wrong? Did I misunderstand the ParcelFileDescriptor? Here is the trace:

01-01 08:06:02.589: E/AndroidRuntime(7483): FATAL EXCEPTION: main 01-01 08:06:02.589: E/AndroidRuntime(7483): java.lang.RuntimeException: Unable to start service com.test.robotsample.MyService@4161a0a8 with Intent { cmp=com.test.robotsample/.MyService }: java.lang.RuntimeException: Not allowed to write file descriptors here 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ActivityThread.access$1900(ActivityThread.java:130) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Handler.dispatchMessage(Handler.java:99) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Looper.loop(Looper.java:137) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ActivityThread.main(ActivityThread.java:4745) 01-01 08:06:02.589: E/AndroidRuntime(7483): at java.lang.reflect.Method.invokeNative(Native Method) 01-01 08:06:02.589: E/AndroidRuntime(7483): at java.lang.reflect.Method.invoke(Method.java:511) 01-01 08:06:02.589: E/AndroidRuntime(7483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 01-01 08:06:02.589: E/AndroidRuntime(7483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-01 08:06:02.589: E/AndroidRuntime(7483): at dalvik.system.NativeStart.main(Native Method) 01-01 08:06:02.589: E/AndroidRuntime(7483): Caused by: java.lang.RuntimeException: Not allowed to write file descriptors here 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Parcel.nativeWriteFileDescriptor(Native Method) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Parcel.writeFileDescriptor(Parcel.java:552) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.ParcelFileDescriptor.writeToParcel(ParcelFileDescriptor.java:412) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Parcel.writeParcelable(Parcel.java:1254) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Parcel.writeValue(Parcel.java:1173) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Parcel.writeMapInternal(Parcel.java:591) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Bundle.writeToParcel(Bundle.java:1619) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.os.Parcel.writeBundle(Parcel.java:605) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.content.Intent.writeToParcel(Intent.java:6470) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ActivityManagerProxy.startService(ActivityManagerNative.java:2468) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ContextImpl.startService(ContextImpl.java:1149) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.content.ContextWrapper.startService(ContextWrapper.java:383) 01-01 08:06:02.589: E/AndroidRuntime(7483): at com.test.robotsample.MyService.onStartCommand(MyService.java:63) 01-01 08:06:02.589: E/AndroidRuntime(7483): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490) 01-01 08:06:02.589: E/AndroidRuntime(7483): ... 10 more

回答1:

Yes, it is not possible to pass a ParcelFileDescriptor through an Intent. Although this does not seem to be clearly documented in the Android docs.

Alternatives:

  • use Bundle.putBinder() to pass a Binder which will return a Parcel with the PFD (from API 18).
  • use a "bound service", started with Context.bindService(), where you again get a Binder through which to obtain the PFD.