1

我是android新手,遇到了一个奇怪的问题...

我正在尝试修改 Google 提供的 WifiDirectDemo,更具体地说,该演示为我们提供了选择我想要传输的图像的自由,但我只想让它传输特定的图像。原代码如下:

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);
}

它工作得很好。但是在我将代码从 onActivityResult 方法移动到 onClick 方法之后,我在运行时遇到了一些错误。这是我的代码。

@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);
}

并且错误代码如下:

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)

有没有人可以帮忙?

更新:

我在清单中添加了权限,清单如下所示:

<?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>

并且应用程序仍然崩溃并显示相同的错误信息。

但是当我运行以下代码时,没有发生错误。

@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);
}

因此,当我将行序列从 onActivityResult 移动到 onClick 时会发生错误。我只是不知道情况是什么,因为实际上我已经在清单中添加了 MANAGE_DOCUMENT 权限,并且它一直在请求权限。而且没有manifest中的权限,如果我把代码放回onActivityResult,我仍然可以访问图像文件。有谁知道为什么?太感谢了!

4

3 回答 3

0

在您的清单中声明许可

   <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
于 2013-11-15T07:24:04.177 回答
0

您需要在清单中添加以下权限

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
于 2013-11-15T07:29:14.750 回答
0

我认为您在清单文件中缺少一些权限将其添加到您的清单文件中

阅读此文档https://developer.android.com/guide/topics/providers/document-provider.html

 <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
于 2013-11-15T07:25:10.973 回答