我正在使用以下方法来调用任何文件,但它无法正常工作。
private void fileIntent(int file)
{
if ((ActivityCompat.checkSelfPermission(ICShowFileCabinetDetails.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, file);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "Select File"), file);
}
}
以下权限在清单
中设置<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
OnActivtyresult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_FILE && data != null) {
try {
mProPic = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
// String filename = selectedImage.getLastPathSegment();
String[] filenames = picturePath.split("\\/");
int count = filenames.length;
String name = filenames[count - 1];
imagepickerselected = 1;
UploadIamgeinServer(1, name);
} catch (IOException e) {
e.printStackTrace();
}
}
}
每当我点击按钮点击选择文件时,选择文件开启。但是所有文件都显示为隐藏,除了图像,点击doest工作。没有方法,按钮点击工作正常。如果有人在代码中发现错误,请告诉我。
由于
答案 0 :(得分:0)
试试这段代码我的代码仅供.xls使用你的代码。 或在清单文件中添加读写权限
oncreate declare permission
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},23
);
}
}
点击你的按钮写下这段代码
path= String.valueOf(Environment.getExternalStorageDirectory());
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");
try {
startActivityForResult(pdfOpenintent.createChooser(intent, "Select file"), 0); }
catch (ActivityNotFoundException e) {
}
onactivityresult yud get file path
public void onActivityResult(int requestCode, int resultCode, Intent result){
if (resultCode == RESULT_OK){
if (requestCode == 0) {
Uri data = result.getData();
else{
// CommonMethods.ShowMessageBox(CraneTrackActivity.this, "Invalid file type");
Toast.makeText(Import_act.this,"Wrong File Selected ", Toast.LENGTH_SHORT).show();
}
}
}
}
答案 1 :(得分:0)
我认为你应该添加像这个intent.addCategory(Intent.CATEGORY_OPENABLE); 。有关更多信息,请访问此链接Select File from file manager via Intent。我希望这对你有帮助。