このインテントを使用して、ファイルを選択するためのセレクターを開きます。
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.parse(getExternalFilesDir(null).toString()), "*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooser = Intent.createChooser(intent, "Choose File");
startActivityForResult(chooser, FILE_SELECT);
私はそれを完全には理解していませんが、うまくいきます。Web
の例からコピーしました結果はURIですが、例からコピーしたURIの処理が面倒すぎることがわかりました
if(uri != null) {
if("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {"_data"};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if(cursor.moveToFirst()) {
return cursor.getString(column_index);
}
}
catch (Exception e) {}
} else if("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}
これは、ユーザーがファイルを選択してそのパスを取得できるようにする最も簡単な方法ですか?
uri が「コンテンツ」かどうかを確認する必要があるのはなぜですか? ユーザーがインテントで選択したテキストまたは圧縮ファイルを開きたいだけです。
「コンテンツ」チェックを無視して、「ファイル」と仮定して単に使用できuri.getPath()
ますか?
編集I:URIスキーム「ファイル」で強制的に選択する方法はありますか?