Android 找不到文件。。。从多媒体资料中选择视频
我两者都有 /文件/视频:5312 及 content://com.android.providers.media.documents/document/video%3A5312 作为我从gallery中选择的视频文件的“URI”或“绝对路径”,并将其输入新文件() 我尝试了上面的两个URI/路径,但它们都不起作用。 不起作用。找不到该文件 File()需要什么格式? 此路径有效。如何使用File()打开它 我从gallery中选择的视频文件的“URI”或“绝对路径” 这些是Android 找不到文件。。。从多媒体资料中选择视频,android,Android,我两者都有 /文件/视频:5312 及 content://com.android.providers.media.documents/document/video%3A5312 作为我从gallery中选择的视频文件的“URI”或“绝对路径”,并将其输入新文件() 我尝试了上面的两个URI/路径,但它们都不起作用。 不起作用。找不到该文件 File()需要什么格式? 此路径有效。如何使用File()打开它 我从gallery中选择的视频文件的“URI”或“绝对路径” 这些是Uri值。它们不是
Uri
值。它们不是文件系统路径,您也无法为它们获取文件系统路径
找不到该文件
这是因为它们不是文件<代码>content://com.android.providers.media.documents/document/video%3A5312的方案是内容
,而不是文件
类似地,https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery
不是文件,因为Uri
具有https
的方案
如何使用File()打开它
你没有,就像你打开https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery
使用文件
相反,就像您可以使用OkHttp或HttpUrlConnection
在https
Uri
上获取InputStream
一样
我从gallery中选择的视频文件的“URI”或“绝对路径”
这些是Uri
值。它们不是文件系统路径,您也无法为它们获取文件系统路径
找不到该文件
这是因为它们不是文件<代码>content://com.android.providers.media.documents/document/video%3A5312的方案是内容
,而不是文件
类似地,https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery
不是文件,因为Uri
具有https
的方案
如何使用File()打开它
你没有,就像你打开https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery
使用文件
相反,就像您可以使用OkHttp或HttpUrlConnection
在https
Uri
上获取InputStream
一样,确定。多谢各位
这就是我所做的
String[] perms = {"android.permission. WRITE_EXTERNAL_STORAGE"};
int permsRequestCode = 200;
requestPermissions(perms, permsRequestCode);
public void convertFile(Uri urx)
throws IOException {
ContentResolver test = getContentResolver();
InputStream initialStream = test.openInputStream(urx);
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);
File xpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
File targetFile = new File(xpath, "/test.mp4");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
uploadFile(targetFile.getPath());
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case 200:
boolean writeAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED;
break;
}
}
我在尝试写入.mp4文件时遇到EACES错误。有没有办法“将其写入内存”?或者您可以解决此问题。好的。多谢各位
这就是我所做的
String[] perms = {"android.permission. WRITE_EXTERNAL_STORAGE"};
int permsRequestCode = 200;
requestPermissions(perms, permsRequestCode);
public void convertFile(Uri urx)
throws IOException {
ContentResolver test = getContentResolver();
InputStream initialStream = test.openInputStream(urx);
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);
File xpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
File targetFile = new File(xpath, "/test.mp4");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
uploadFile(targetFile.getPath());
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case 200:
boolean writeAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED;
break;
}
}
我在尝试写入.mp4文件时遇到EACES错误。有没有办法“将其写入内存”?或者你可以解决这个问题。你的问题太模糊了,有点。。更好?我真的没有得到你想要的?可能有用或相关。瞧,你的问题有点含糊不清。。更好?我真的没有得到你想要的?可能有用或相关。请看批准的答案。你能帮我举一个用这种方法上传文件的例子吗。。。?我在谷歌上只找到了一个Xamarin的应用程序。@DavidKachlon:好吧,很少有HTTP客户端API完全依靠
InputStream
工作,以防它们需要重新启动HTTP操作。您必须查看您选择的HTTP API是否支持InputStream
(例如,OkHttp不支持)。如果是的话,那太好了。如果没有,也可以在某处打开一个FileOutputStream
(例如getCacheDir()
),并将字节从InputStream
复制到FileOutputStream
。然后,您可以将内容副本用作文件,在完成后将其删除。您能帮我举一个使用此方法上载文件的示例吗。。。?我在谷歌上只找到了一个Xamarin的应用程序。@DavidKachlon:好吧,很少有HTTP客户端API完全依靠InputStream
工作,以防它们需要重新启动HTTP操作。您必须查看您选择的HTTP API是否支持InputStream
(例如,OkHttp不支持)。如果是的话,那太好了。如果没有,也可以在某处打开一个FileOutputStream
(例如getCacheDir()
),并将字节从InputStream
复制到FileOutputStream
。然后,您可以将内容副本用作文件,完成后将其删除。