onActivityResult 中的 FileNotFoundException
FileNotFoundException in onActivityResult
我试图只浏览两种文件类型:图像或 pdf。
这是来源:
String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
myPermissions =new MyPermissions(TestDialog.this, 0, permissions);
MyPermissions.EventHandler permHandler = new MyPermissions.EventHandler() {
@Override
public void handle() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.setType("image/jpeg");
startActivityForResult(intent, 0);
}
};
myPermissions.doIfHasPermissions(permHandler);
这是我的 onActivityResult
来源:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String url = data.getData().getPath();
File myFile = new File(url);
Log.e("base64 ", getStringFile(myFile));
}
super.onActivityResult(requestCode, resultCode, data);
}
public String getStringFile(File f) {
InputStream inputStream = null;
String encodedFile = "", lastVal;
try {
inputStream = new FileInputStream(f.getAbsolutePath());
byte[] buffer = new byte[10240];//specify the size to allow
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
while ((bytesRead = inputStream.read(buffer)) != -1) {
output64.write(buffer, 0, bytesRead);
}
output64.close();
encodedFile = output.toString();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
lastVal = encodedFile;
return lastVal;
}
我想将所选文件转换为 Base64,但我得到 FileNotFoundException
。有谁知道我做错了什么吗?
I try to browse only two type files,images or pdf
您的代码与文件无关。它使用ACTION_GET_CONTENT
,允许用户选择一段内容。
String url = data.getData().getPath();
这条线没用,除非Uri
有file
的方案。最有可能的是,它有一个 content
.
的方案
停止使用 File
和 FileInputStream
。相反,从 ContentResolver
(来自 getContentResolver()
)及其 openInputStream()
方法中获取 InputStream
。你可以传入Uri
,不管Uri
方案是file
还是content
,你都会得到一个InputStream
。
另请注意,您的应用可能会因 OutOfMemoryError
而崩溃,除了相当小的文件,因为您没有足够的堆 space 来执行此转换。
看看
Uri uri = data.getData();
然后尝试记录 uri.toString() 的值。
您会看到它以 "content//...." 开头。
不要尝试查找文件。
使用 InputStream 代替 FileInputStream。
InputStream inputStream = getContentResolver().openInputStream(uri);
我试图只浏览两种文件类型:图像或 pdf。
这是来源:
String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
myPermissions =new MyPermissions(TestDialog.this, 0, permissions);
MyPermissions.EventHandler permHandler = new MyPermissions.EventHandler() {
@Override
public void handle() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.setType("image/jpeg");
startActivityForResult(intent, 0);
}
};
myPermissions.doIfHasPermissions(permHandler);
这是我的 onActivityResult
来源:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String url = data.getData().getPath();
File myFile = new File(url);
Log.e("base64 ", getStringFile(myFile));
}
super.onActivityResult(requestCode, resultCode, data);
}
public String getStringFile(File f) {
InputStream inputStream = null;
String encodedFile = "", lastVal;
try {
inputStream = new FileInputStream(f.getAbsolutePath());
byte[] buffer = new byte[10240];//specify the size to allow
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
while ((bytesRead = inputStream.read(buffer)) != -1) {
output64.write(buffer, 0, bytesRead);
}
output64.close();
encodedFile = output.toString();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
lastVal = encodedFile;
return lastVal;
}
我想将所选文件转换为 Base64,但我得到 FileNotFoundException
。有谁知道我做错了什么吗?
I try to browse only two type files,images or pdf
您的代码与文件无关。它使用ACTION_GET_CONTENT
,允许用户选择一段内容。
String url = data.getData().getPath();
这条线没用,除非Uri
有file
的方案。最有可能的是,它有一个 content
.
停止使用 File
和 FileInputStream
。相反,从 ContentResolver
(来自 getContentResolver()
)及其 openInputStream()
方法中获取 InputStream
。你可以传入Uri
,不管Uri
方案是file
还是content
,你都会得到一个InputStream
。
另请注意,您的应用可能会因 OutOfMemoryError
而崩溃,除了相当小的文件,因为您没有足够的堆 space 来执行此转换。
看看
Uri uri = data.getData();
然后尝试记录 uri.toString() 的值。
您会看到它以 "content//...." 开头。
不要尝试查找文件。
使用 InputStream 代替 FileInputStream。
InputStream inputStream = getContentResolver().openInputStream(uri);