我正在尝试从Android设备读取文件并将其上传到服务器,我的代码如下:
MultipartUtility multipart = new MultipartUtility(ForSaleConstants.REVIEW_IMAGE_UPLOADER, NetworkManager.CHARACTER_SET);
multipart.addFormField(ForSaleConstants.PHONE, phone);
String uriString = extraFileUri.toString();
Log.e("File_URI", uriString);
uriString = Uri.decode(uriString);
Log.e("File_URI2", uriString);
if (uriString.contains("file://")) {
uriString = uriString.replace("file://", "");
File uploadFile = new File(uriString);
multipart.addFilePart(("image"), uploadFile);
} else if (uriString.contains("content://")) {
uriString = FileManager.getInstance().getRealPathFromImageUri(context, Uri.parse(uriString));
File uploadFile = new File(uriString);
multipart.addFilePart(("image"), uploadFile);
} else {
try {
File uploadFile = new File(uriString);
multipart.addFilePart(("image"), uploadFile);
} catch (Exception e) {
e.printStackTrace();
}
}
我遇到了以下崩溃:
08-14 15:33:35.765 22931-23288/com.forsale.forsale W/System.err: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image:3954 from pid=22931, uid=10365 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
08-14 15:33:35.778 22931-23288/com.forsale.forsale W/System.err: at android.os.Parcel.readException(Parcel.java:1693)
08-14 15:33:35.778 22931-23288/com.forsale.forsale W/System.err: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
08-14 15:33:35.778 22931-23288/com.forsale.forsale W/System.err: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
08-14 15:33:35.778 22931-23288/com.forsale.forsale W/System.err: at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
08-14 15:33:35.778 22931-23288/com.forsale.forsale W/System.err: at android.content.ContentResolver.query(ContentResolver.java:536)
08-14 15:33:35.778 22931-23288/com.forsale.forsale W/System.err: at android.content.ContentResolver.query(ContentResolver.java:478)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at com.technivance.q84sale.base.controller.manager.FileManager.getRealPathFromImageUri(FileManager.java:225)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at com.forsale.forsale.services.controller.manager.ServicesManager$29.doInBackground(ServicesManager.java:2797)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at com.forsale.forsale.services.controller.manager.ServicesManager$29.doInBackground(ServicesManager.java:2774)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
08-14 15:33:35.779 22931-23288/com.forsale.forsale W/System.err: at java.lang.Thread.run(Thread.java:762)
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
答案 0 :(得分:0)
在链接下方使用运行时权限获取运行时权限代码,并在Splash或您使用相机的活动上询问
http://www.theappguruz.com/blog/runtime-permissions-in-android-marshmallow
答案 1 :(得分:0)
您必须在Android 6.0(API级别23)中获得/授予运行时权限
了解更多信息:https://developer.android.com/training/permissions/requesting.html