title: App crashes when starting service in onClick method
body: <p>I am a newbie to android and I got a weird problem...</p>
  
  <p>I am trying to modify the WifiDirectDemo provided by Google, more specifically, the demo offers us the freedom to choose a image I want to transfer but I just want it to transfer a specific image. The original code is as follows:</p>
  
  <pre><code>public class DeviceDetailFragment extends Fragment implements ConnectionInfoListener {
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      mContentView = inflater.inflate(R.layout.device_detail, null);
  
      mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
              new View.OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      // Allow user to pick an image from Gallery or other
                      // registered apps                  
                      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                      intent.setType("image/*");
                      startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
                  }
              });
      return mContentView;
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      // User has picked an image. Transfer it to group owner i.e peer using
      // FileTransferService.
      Uri uri = data.getData();
      TextView statusText = (TextView) mContentView.findViewById(R.id.status_text);
      statusText.setText("Sending: " + uri);
      Log.d(WiFiDirectActivity.TAG, "Intent----------- " + uri);
      Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
      serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
      serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uri.toString());
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
              info.groupOwnerAddress.getHostAddress());
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
      getActivity().startService(serviceIntent);
  }
  </code></pre>
  
  <p>And it works totally fine. But after I move the code from onActivityResult method into onClick method, I got some errors in the runtime. Here is my code.</p>
  
  <pre><code>@Override
  public void onClick(View v) {
                      // Allow user to pick an image from Gallery or other
                      // registered apps               
      String uristring = "content://com.android.providers.media.documents/document/image%3A25";
      Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
      serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
      serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uristring);
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
              info.groupOwnerAddress.getHostAddress());
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
      getActivity().startService(serviceIntent);
  }
  </code></pre>
  
  <p>And the error code is as follows:</p>
  
  <pre><code>11-15 00:11:16.397: W/dalvikvm(18595): threadid=11: thread exiting with uncaught exception (group=0x41553b90)
  11-15 00:11:16.397: E/AndroidRuntime(18595): FATAL EXCEPTION: IntentService[FileTransferService]
  11-15 00:11:16.397: E/AndroidRuntime(18595): Process: com.example.android.wifidirect, PID: 18595
  11-15 00:11:16.397: E/AndroidRuntime(18595): java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{42a042a8 18595:com.example.android.wifidirect/u0a77} (pid=18595, uid=10077) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Parcel.readException(Parcel.java:1461)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Parcel.readException(Parcel.java:1415)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2848)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.ActivityThread.acquireProvider(ActivityThread.java:4396)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2207)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1047)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at com.example.android.wifidirect.FileTransferService.onHandleIntent(FileTransferService.java:63)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Handler.dispatchMessage(Handler.java:102)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Looper.loop(Looper.java:137)
  11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.HandlerThread.run(HandlerThread.java:61)
  </code></pre>
  
  <p>Is there anyone can help?</p>
  
  <p>Update:</p>
  
  <p>I added the permission to the manifest, here is how the manifest looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.wifidirect"
  android:versionCode="1" android:versionName="1.0"&gt;
  
  &lt;uses-sdk android:minSdkVersion="14" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/&gt;
  
  &lt;!-- Google Play filtering --&gt; 
  &lt;uses-feature android:name="android.hardware.wifi.direct" android:required="true"/&gt;
  
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@android:style/Theme.Holo"&gt;
      &lt;activity
          android:name=".WiFiDirectActivity"
          android:label="@string/app_name" android:launchMode="singleTask"&gt;
          &lt;intent-filter&gt;
              &lt;action
                  android:name="android.intent.action.MAIN" /&gt;
              &lt;category
                  android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;!-- Used for transferring files  after a successful connection --&gt;
      &lt;service android:enabled="true" android:name=".FileTransferService" /&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>And the app still crashes with the same error information.</p>
  
  <p>But when I run the following code, no error occurs.</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
     mContentView = inflater.inflate(R.layout.device_detail, null);
     mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
              new View.OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      // Allow user to pick an image from Gallery or other
                      // registered apps
  
                      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                      intent.setType("image/*");
                      startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
                  }
              });
  
      return mContentView;
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
  
      // User has picked an image. Transfer it to group owner i.e peer using
  
      String uristring = "content://com.android.providers.media.documents/document/image%3A25";
  
      Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
      serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
      serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uristring);
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
              info.groupOwnerAddress.getHostAddress());
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
      getActivity().startService(serviceIntent);
  }
  </code></pre>
  
  <p>So errors occur when I move the sequence of lines from onActivityResult to onClick. I just don't know what the situation is because actually I already added the MANAGE_DOCUMENT permission to the manifest and it keeps asking for permission. And without the permission in manifest, I can still access the image file if I put the code back to onActivityResult. Does anyone know why? Thank you so much!</p>