我正在尝试从文件管理器中检索文件(任何类型)并将该文件编码为 Base64 字符串。我找到了很多涉及图像的答案,但我需要任何类型的文件。他就是我正在做的。
我正在像这样从画廊中检索文件(任何类型)
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose file"), GALLERY_REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)
在我的结果中
Uri returnUri = intent.getData();
Run Code Online (Sandbox Code Playgroud)
什么给了我 'content://com.android.providers.downloads.documents/document/1646'
然后我试试
File file = new File( returnUri.getPath() );
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,但随后我尝试将文件编码为 Base64 字符串:
String str = null;
try {
str = convertFile(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
和 convertFile 方法
public String convertFile(File file)
throws IOException {
byte[] bytes = loadFile(file);
byte[] encoded = Base64.encode(bytes, Base64.DEFAULT);
String encodedString = new String(encoded);
return encodedString; …Run Code Online (Sandbox Code Playgroud) 我有一个 EditText 不允许用户通过键盘(软或硬)输入任何内容。这个 EditText 应该只允许用户通过应用程序在屏幕上显示的键(按钮)输入一些东西。我禁用了软键盘,但找不到禁用硬件键盘输入的方法。这种通过硬件键盘的输入可以使用配置为允许通过硬件键盘输入的模拟器来完成。所以,我的问题是,如何在 EditText 中通过物理键盘阻止输入?谢谢!