I am a newbie to android and I got a weird problem...
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:
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);
}
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.
@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);
}
And the error code is as follows:
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)
Is there anyone can help?
Update:
I added the permission to the manifest, here is how the manifest looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.wifidirect"
android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<!-- Google Play filtering -->
<uses-feature android:name="android.hardware.wifi.direct" android:required="true"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo">
<activity
android:name=".WiFiDirectActivity"
android:label="@string/app_name" android:launchMode="singleTask">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Used for transferring files after a successful connection -->
<service android:enabled="true" android:name=".FileTransferService" />
</application>
And the app still crashes with the same error information.
But when I run the following code, no error occurs.
@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);
}
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!