Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在最新的API中从多媒体资料中挑选视频?_Android_Video_Gallery - Fatal编程技术网

Android 如何在最新的API中从多媒体资料中挑选视频?

Android 如何在最新的API中从多媒体资料中挑选视频?,android,video,gallery,Android,Video,Gallery,我试图获取库中视频的路径,但URI给了我类似“/document/video:xxxx”的信息。当我使用getPath()时,我总是得到null 我已经设置了一个文件提供程序并授予了所有权限,但没有任何效果 //Start intent Intent intent = new Intent(); intent.setType("video/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startAct

我试图获取库中视频的路径,但URI给了我类似“/document/video:xxxx”的信息。当我使用getPath()时,我总是得到null

我已经设置了一个文件提供程序并授予了所有权限,但没有任何效果

    //Start intent

    Intent intent = new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Video"), 1);

    //Turn URI to Path

    String getPath(Uri uri) {

    String[] projection = { MediaStore.Video.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

    if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL. THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else {
        return null;
    }

    }

这将允许您从多媒体资料中选择视频:

 Intent intent = new Intent(Intent.ACTION_PICK,   MediaStore.Video.Media.EXTERNAL_CONTENT_URI);

有关
意图
MediaStore
的更多信息,请阅读在build.gradle(应用程序)中添加以下依赖项:

设置widget clicklistener并创建文件的数组imgbase64:

private var imageBase64 = ArrayList<File>()

imgVideo.setOnClickListener {
            val intent1 = Intent(this, VideoPickActivity::class.java)
            intent1.putExtra(ImagePickActivity.IS_NEED_CAMERA, false)
            intent1.putExtra(Constant.MAX_NUMBER, 1)
            intent1.putExtra(BaseActivity.IS_NEED_FOLDER_LIST, false)
            startActivityForResult(intent1, Constant.REQUEST_CODE_PICK_VIDEO)
        }
private var imageBase64=ArrayList()
imgVideo.setOnClickListener{
val intent1=Intent(这是VideoPickActivity::class.java)
intent1.putExtra(ImagePickActivity.IS\u NEED\u CAMERA,false)
intent1.putExtra(常量最大值,1)
intent1.putExtra(BaseActivity.IS\u NEED\u FOLDER\u LIST,false)
startActivityForResult(intent1,常量。请求\代码\拾取\视频)
}
OVERRIDE onActivityResult()

override-on-activityresult(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据)
如果(数据!=null){
试一试{
imageBase64.clear()
何时(请求代码){
AppConstants.REQUEST\u CODE\u PICK\u VIDEO->{
if(resultCode==Activity.RESULT\u OK&&data!=null){
val list=data.getParcelableArrayListExtra(AppConstants.RESULT\u PICK\u VIDEO)
用于(列表中的项目){
imageBase64.add(文件(item.path))
文件类型=2
}
}
}
}
}捕获(e:IOException){
e、 printStackTrace()
}
}
}
创建多部分的surveyImagesParts数组

val surveyImagesParts: Array<MultipartBody.Part?> = arrayOfNulls(imageBase64.size)
                for (index in imageBase64.indices) {
                    surveyImagesParts[index] = MultipartBody.Part.createFormData(
                        "message", imageBase64[index].name,
                        RequestBody.create(MediaType.parse("*/*"), imageBase64[index])
                    )
                }
val surveyImagesParts:Array=arrayOfNulls(imageBase64.size)
for(imageBase64.index中的索引){
surveyImagesParts[索引]=MultipartBody.Part.createFormData(
“消息”,imageBase64[索引]。名称,
create(MediaType.parse(“*/*”),imageBase64[index])
)
}
在api调用中传递surveyImagesParts。。。
希望它能帮助你

拾取部分不是问题所在,您将获得一个URI。问题是默认情况下,路径没有意义,因为您将无法访问它

MediaStore.Video.Media.DATA

此常量在API级别29中被弃用。 应用程序可能没有直接访问此路径的文件系统权限。应用程序应该使用ContentResolver#openFileDescriptor(Uri,字符串)来获得访问权限,而不是试图直接打开此路径

即使您能够获得路径,那么默认情况下您也没有使用它的权限,您需要使用
ContentResolver#openFileDescriptor(Uri,String)
来获得访问权限

我建议您阅读最新API上的存储

更新:添加了如何处理
Intent
调用的结果并获取可传递给大多数Java IO操作的Java
FileDescriptor
的示例

@覆盖
ActivityResult上受保护的void(int请求代码、int结果代码、意图结果数据){
super.onActivityResult(请求代码、结果代码、结果数据);
Uri=null;
//问题代码中的'requestCode'是'1'
if(requestCode==1&&resultCode==RESULT\u确定){
if(resultData!=null){
uri=resultData.getData();
试一试{
//开放阅读
包封描述符pfd=
此文件名为.getContentResolver()。
openFileDescriptor(uri,“r”);
FileDescriptor FileDescriptor=pfd.getFileDescriptor();
//现在执行一些fileDescriptor操作,比如读取输入流
文件输入流文件输入流=
新文件输入流(
文件描述符);
....
}捕获(例外e){
//做一些例外的事情
}
}
}
}

在清单文件中使用下面的permission,如果您的设备是棉花糖和棉花糖以上的,则还应授予运行时权限

         <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


你文章的主题与文章中描述的问题无关。请调整。我得到了正确的路径,但它给了我
java.nio.file.AccessDeniedException:/storage/simulated/0/DCIM/Camera/VID_20191105\u 203230.mp4
请添加存储依赖权限请遵循以下链接:它可能会对您有所帮助。更新后添加了一些示例代码,说明如何获取
文件描述符
再次感谢您,该文件应该是视频,我可以将这些字节写入新文件(副本)吗?是的,您可以写入文件,但根据您想保存文件的位置,您可能需要使用
MediaStore
Storage Access Framework
ACTION\u CREATE\u DOCUMENT
来创建文件,但同样,您将无法使用这些方法访问路径。我会将其保存到getExternalFilesDir()。我该怎么做?我建议你问另一个问题,因为写文件超出了这个问题的范围
val surveyImagesParts: Array<MultipartBody.Part?> = arrayOfNulls(imageBase64.size)
                for (index in imageBase64.indices) {
                    surveyImagesParts[index] = MultipartBody.Part.createFormData(
                        "message", imageBase64[index].name,
                        RequestBody.create(MediaType.parse("*/*"), imageBase64[index])
                    )
                }
         <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>