我想在按钮点击时从手机中选择pdf文件,并在文本视图中显示其文件名。到目前为止,我已经完成了打开文件管理器,点击按钮
选择pdfbtnUpload.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);
}
});
如何在textview
??
答案 0 :(得分:13)
在 startActivity(intent); 的地方使用 startActivityForResult(intent,1212),然后执行 onActivityResult
中的后续操作@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1212:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
String displayName = null;
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
答案 1 :(得分:2)
添加getPDFPath
方法:
public String getPDFPath(Uri uri){
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
在您的情况下,此代码专门用于DonwloadProvider的文档,以进一步实施检查Paul Burke answer。我个人使用他的aFileChooser library来避免这种问题。
希望这有帮助!
答案 2 :(得分:1)
您可以执行以下操作:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String fileName = getFileName(this, data.getData());
yourTextView.setText(fileName);
}
public String getFileName(Context context, Uri uri) {
if (uri != null && context != null) {
Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
if(returnCursor != null) {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
if(nameIndex >= 0 && sizeIndex >= 0){
Log.d("File Name : " + returnCursor.getString(nameIndex));
Log.d("File Size : " + Long.toString(returnCursor.getLong(sizeIndex)));
Boolean isValidFile = checkFormat(returnCursor.getString(nameIndex));
if(!isValidFile){
return returnCursor.getString(nameIndex);
}
}
private Boolean checkOtherFileType(String filePath){
if(!Utility.isNullOrEmpty(filePath)) {
String filePathInLowerCase = filePath.toLowerCase();
if(filePathInLowerCase.endsWith(".pdf")) {
return true;
}
}
return false;
}
希望这会帮助你