文件uri是已知的,例如
`file:///mnt/sdcard/Download/AppSearch_2213333_60.apk`
Run Code Online (Sandbox Code Playgroud)
我想检查这个文件是否可以在后台打开,怎么办?
当我从内部存储(图库)获取图像时,这是我的代码.在棒棒糖文件路径中,返回始终为null.
if (requestCode == PICK_IMAGE) {
if(resultCode == RESULT_OK){
//image successfully picked
// launching upload activity
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnindex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
file_path = cursor.getString(columnindex);
Log.d(getClass().getName(), "file_path"+file_path);
fileUri = Uri.parse("file://" + file_path);
cursor.close();
launchUploadActivity(true, PICK_IMAGE);
}else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),"User cancelled image selection", Toast.LENGTH_SHORT).show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),"Sorry! failed to pick image", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud) android filepath imageview internal-storage android-5.0-lollipop
我正在启动文件选择器,用户可以在其中选择要使用的pdf文件并将其上传到服务器。
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,1);
Run Code Online (Sandbox Code Playgroud)
当他们选择文件时,它会返回到onActivityResult我查看Uri文件的位置,以查看文件是否包含.pdf
if(requestCode == 1 && resultCode == RESULT_OK){
Uri filePath = data.getData();
File file = new File(filePath.getPath());
if(filePath.toString().contains(".pdf")){
if(file.length() <= 1048576){
de.setPDFUri(filePath.toString());
}else{
Toast.makeText(this,"PDF cannot be more than 1 MB, please select another", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(this,"Can only upload PDF files", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这仅适用于某些文件管理器应用程序(Astro,Explorer),大多数时候我会得到这样的回报
file:///storage/sdcard0/Download/20131231103232738561744.pdf
Run Code Online (Sandbox Code Playgroud)
在其他程序上(到目前为止,我仅见过制造商内置的应用程序),我得到了这样的回报
content://media/external/file/109
Run Code Online (Sandbox Code Playgroud)
这显然不会告诉我有关文件及其类型的任何信息,并且不会返回该ContentProvider文件的信息。
有没有一种方法可以让用户仅.pdf通过发送意图来选择文件,或者有一种方法可以检查uri并确保它是.pdf文件?
我正在尝试为FileInputStream用户从他们的图库中选择的图片获取一个对象,当我尝试从我收到的URI中打开文件时,它一直在说FileNotFoundException......
这是我用来启动从图库中选择图像的意图的代码:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
Run Code Online (Sandbox Code Playgroud)
这是我用来捕捉的代码onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
File myFile = new File(uri.getPath());
FileInputStream fiStream = new FileInputStream(myFile);
...
}
}
Run Code Online (Sandbox Code Playgroud)
在创建的时候FileInputStream,我通过模拟器选择了一张照片时出现了以下错误:
W/System.err: java.io.FileNotFoundException: /document/image:26171: open failed: ENOENT (No such file or …Run Code Online (Sandbox Code Playgroud) 我想在按钮单击时从手机中选择pdf文件,并在文本视图中显示其文件名.到目前为止,我已经完成了打开文件管理器,点击按钮选择pdf
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivity(intent);
}
});
Run Code Online (Sandbox Code Playgroud)
如何获取所选的文件名textview?
我想选择存在于SD卡中的文件而不是内部存储器并上传它的服务器,但是我无法获得其获取其大小的路径.我已经开始使用以下代码选择文件的意图:
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
Run Code Online (Sandbox Code Playgroud)
为获取文件的路径,我使用此答案及其工作,除非用户从sdcard(可移动)中选择任何文件.当我调试代码并发现该类型不是主要类型时,它不会进入这种情况:
if("primary".equalsIgnoreCase(type)){
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是它还有什么呢?即如果类型不是主要的呢?在这种情况下我们如何获得文件路径?我已经搜索了许多问题和教程,其中任何一个都没有.我还尝试了这个答案的其他部分,但它不起作用,因为" SECONDARY_STORAGE " System.getenv()返回null," EXTERNAL_STORAGE " 返回sdcard.我尝试时遇到文件未找到异常:
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}else{
return System.getenv("EXTERNAL_STORAGE") + "/" + split[1];
}
Run Code Online (Sandbox Code Playgroud)
文件的Uri和doc Id看起来像:
Uri:content://com.android.externalstorage.documents/document/0EF9-3110%3Adevice-2016-12-02-130553.png
docId:0EF9-3110:device-2016-12-02-130553.png
任何帮助?
我正在使用Intent.ACTION_GET_CONTENT它打开最近的文件。从最近的文件中选择项目会产生错误的 URI,但从文件管理器中选择相同的文件会提供正确的 URI,可以由我的代码处理。
public static String getRealPathFromURI(Context context, Uri uri) {
String path;
if ("content".equals(uri.getScheme())) {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
path = cursor.getString(idx);
cursor.close();
} else {
path = uri.getPath();
}
return path;
}
Run Code Online (Sandbox Code Playgroud)
注意:uri.getPath()当我从最近的文件中选择 PDF 时的输出是/document/...但从文件管理器中选择相同的文件时,.../emulated/....
注意:从最近的文件中选择文件时的错误是
无法从 CursorWindow 读取第 0 行、第 -1 行。在访问 Cursor 中的数据之前,确保它已正确初始化。
android android-intent android-contentresolver android-cursor
我正在尝试选择一个视频,选择并显示视频效果很好,但是当我尝试获取视频文件的完整路径时,我得到了错误的路径。
在下面的代码片段中,我尝试获取文件的完整路径:
Uri uri = Uri.parse(url);
File file = new File((uri.getPath()));
Run Code Online (Sandbox Code Playgroud)
然后,当我设置多部分实体文件主体时,我将file.toString().
尝试上传文件时出现以下异常。
03-28 12:43:27.129: W/System.err(6018): java.io.FileNotFoundException: /content:/media/external/video/media/32398: open failed: ENOENT (No such file or directory)
03-28 12:43:27.157: W/System.err(6018): at libcore.io.IoBridge.open(IoBridge.java:416)
03-28 12:43:27.157: W/System.err(6018): at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-28 12:43:27.157: W/System.err(6018): at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:92)
03-28 12:43:27.165: W/System.err(6018): at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206)
03-28 12:43:27.165: W/System.err(6018): at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224)
03-28 12:43:27.169: W/System.err(6018): at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183)
03-28 12:43:27.169: W/System.err(6018): at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)
03-28 12:43:27.169: W/System.err(6018): at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162)
Run Code Online (Sandbox Code Playgroud) 当用户点击谷歌硬盘中的"发送文件"按钮并选择我的应用程序.我想获取该文件的文件路径,然后允许用户将其上传到其他位置.
我为kitkat手机检查了这些类似的SO帖子:从URI,Android KitKat新的存储访问框架获取真实路径
然而,解决方案似乎不再适用于Lollipop设备.
问题似乎是MediaStore.MediaColumns.DATA在ContentResolver上运行查询时返回null.
https://code.google.com/p/android/issues/detail?id=63651
您应该使用ContentResolver.openFileDescriptor()而不是尝试获取原始文件系统路径."_data"列不是CATEGORY_OPENABLE合同的一部分,因此不需要Drive返回它.
我已经阅读了CommonsWare的这篇博文,其中建议我"尝试直接使用Uri直接使用ContentResolver",这是我不明白的.如何直接在ContentResolvers中使用URI?
但是,我仍然不清楚如何最好地处理这些类型的URI.
我能找到的最佳解决方案是调用openFileDescriptor,然后将文件流复制到一个新文件中,然后将该新文件路径传递给我的上传活动.
private static String getDriveFileAbsolutePath(Activity context, Uri uri) {
if (uri == null) return null;
ContentResolver resolver = context.getContentResolver();
FileInputStream input = null;
FileOutputStream output = null;
String outputFilePath = new File(context.getCacheDir(), fileName).getAbsolutePath();
try {
ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
FileDescriptor fd = pfd.getFileDescriptor();
input = new FileInputStream(fd);
output = new FileOutputStream(outputFilePath);
int read = 0;
byte[] bytes = new byte[4096];
while ((read …Run Code Online (Sandbox Code Playgroud) android android-intent google-drive-api storage-access-framework
我使用以下命令启动图像选择器意图:
final Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, PICK_IMAGE);
Run Code Online (Sandbox Code Playgroud)
然后onActivityResult()我得到所有选择的图像的uris,并启动在后台运行的Jobs并上传这些图像(https://github.com/yigit/android-priority-jobqueue)。但是,如果我按后退按钮并退出活动,那么任何未启动的作业都无法在运行时访问选择的图像并引发异常:
java.lang.SecurityException:权限拒绝:未从uid 10123导出的ProcessRecord {...}(pid = 2407,uid = 10117)打开提供程序com.google.android.apps.photos.contentprovider.MediaContentProvider
发生这种情况的原因是,一旦活动完成,权限就会被撤销。根据文档https://developer.android.com/guide/topics/providers/content-provider-basics.html:
这些是对特定内容URI的权限,该权限将持续到接收它们的活动完成为止。
我的问题是,是否有解决方法?喜欢在应用程序级别获得许可?
有哪些替代方案可以解决此问题?一种快速的解决方案似乎是复制所有拾取的图像,然后上传它们,但这似乎是不得已的方法。
android permission-denied android-contentprovider android-permissions