我在这个link上关注一个从android到服务器的教程上传视频。但是,当我执行此活动时,它给了我运行时异常。根据例外情况,我在manifest
文件中添加了权限。但仍然得到错误。这是我的活动文件
public class UploadActivity extends AppCompatActivity implements View.OnClickListener {
private Button buttonChoose;
private Button buttonUpload;
private TextView textView;
private TextView textViewResponse;
private static final int SELECT_VIDEO = 3;
private String selectedPath;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
report = getIntent().getStringExtra("report");
buttonChoose = (Button) findViewById(R.id.buttonChoose);
buttonUpload = (Button) findViewById(R.id.buttonUpload);
textView = (TextView) findViewById(R.id.textView);
textViewResponse = (TextView) findViewById(R.id.textViewResponse);
buttonChoose.setOnClickListener(this);
buttonUpload.setOnClickListener(this);
}
private void chooseVideo() {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select a Video "), SELECT_VIDEO);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
System.out.println("SELECT_VIDEO");
Uri selectedImageUri = data.getData();
selectedPath = getPath(selectedImageUri);
textView.setText(selectedPath);
}
}
}
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
cursor.close();
return path;
}
private void uploadVideo() {
class UploadVideo extends AsyncTask<Void, Void, String> {
ProgressDialog uploading;
@Override
protected void onPreExecute() {
super.onPreExecute();
uploading = ProgressDialog.show(UploadActivity.this, "Uploading File", "Please wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
uploading.dismiss();
textViewResponse.setText(Html.fromHtml("<b>Uploaded at <a href='" + s + "'>" + s + "</a></b>"));
textViewResponse.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected String doInBackground(Void... params) {
UploadFile u = new UploadFile();
String msg = u.uploadVideo(selectedPath);
return msg;
}
}
UploadVideo uv = new UploadVideo();
uv.execute();
}
@Override
public void onClick(View v) {
if (v == buttonChoose) {
chooseVideo();
}
if (v == buttonUpload) {
uploadVideo();
}
}
}
这是logcat
03-16 09:00:28.658 5832-5832/com.example.luke.upload E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.luke.upload, PID: 5832
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/video:21079 flg=0x1 }} to activity {com.example.luke.upload/com.example.luke.upload.UploadActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media from pid=5832, uid=10234 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media from pid=5832, uid=10234 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1620)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:502)
at android.content.ContentResolver.query(ContentResolver.java:445)
at com.example.luke.upload.UploadActivity.getPath(UploadActivity.java:63)
at com.example.luke.upload.UploadActivity.onActivityResult(UploadActivity.java:52)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我在棉花糖上测试它 谢谢。
答案 0 :(得分:1)
试试这个,
private static final int REQUEST_STORAGE = 112;
@Override
public void onClick(View v) {
if (v == buttonChoose) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE );
} else {
chooseVideo();
}
} else {
chooseVideo();
}
}
if (v == buttonUpload) {
uploadVideo();
}
}
获取权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
chooseVideo();
} else {
Toast.makeText(mContext, "The app was not allowed to write to your storage.", Toast.LENGTH_LONG).show();
}
}
}
}
检查marshmallow的权限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
清单
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />