ファイルアップローダーを作りたいです。したがって、ファイルチューザーが必要ですが、これを自分で書きたくありません。OI file manager を見つけました。自分に合っていると思います。しかし、どうすればユーザーに OI ファイル マネージャーをインストールさせることができますか? できない場合、アプリにファイル マネージャーを含めるより良い方法はありますか? どうも
2 に答える
編集(2012年1月2日):
このプロセスを合理化すると同時に、組み込みのファイルエクスプローラーを提供する小さなオープンソースのAndroidライブラリプロジェクトを作成しました(ユーザーがファイルエクスプローラーを持っていない場合)。使い方は非常に簡単で、数行のコードしか必要ありません。
GitHub: aFileChooserで見つけることができます。
オリジナル
ユーザーがシステム内の任意のファイルを選択できるようにする場合は、独自のファイルマネージャーを含めるか、ユーザーにファイルをダウンロードするようにアドバイスする必要があります。あなたができる最善のことは、次のIntent.createChooser()
ような「開くことができる」コンテンツを探すことだと思います。
private static final int FILE_SELECT_CODE = 0;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
Uri
次に、次のように、選択したファイルをリッスンしますonActivityResult()
。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d(TAG, "File Uri: " + uri.toString());
// Get the path
String path = FileUtils.getPath(this, uri);
Log.d(TAG, "File Path: " + path);
// Get the file instance
// File file = new File(path);
// Initiate the upload
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
私のgetPath()
方法FileUtils.java
は次のとおりです。
public static String getPath(Context context, Uri uri) throws URISyntaxException {
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) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
私はこの目的のために AndExplorer を使用しました。私の解決策は、ダイアログをポップアップし、市場にリダイレクトして不足しているアプリケーションをインストールすることです。
私の startCreation は、外部ファイル/ディレクトリ ピッカーを呼び出そうとしています。見つからない場合は、show installResultMessage 関数を呼び出します。
private void startCreation(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
Uri startDir = Uri.fromFile(new File("/sdcard"));
intent.setDataAndType(startDir,
"vnd.android.cursor.dir/lysesoft.andexplorer.file");
intent.putExtra("browser_filter_extension_whitelist", "*.csv");
intent.putExtra("explorer_title", getText(R.string.andex_file_selection_title));
intent.putExtra("browser_title_background_color",
getText(R.string.browser_title_background_color));
intent.putExtra("browser_title_foreground_color",
getText(R.string.browser_title_foreground_color));
intent.putExtra("browser_list_background_color",
getText(R.string.browser_list_background_color));
intent.putExtra("browser_list_fontscale", "120%");
intent.putExtra("browser_list_layout", "2");
try{
ApplicationInfo info = getPackageManager()
.getApplicationInfo("lysesoft.andexplorer", 0 );
startActivityForResult(intent, PICK_REQUEST_CODE);
} catch( PackageManager.NameNotFoundException e ){
showInstallResultMessage(R.string.error_install_andexplorer);
} catch (Exception e) {
Log.w(TAG, e.getMessage());
}
}
このメソッドはダイアログを選択するだけで、ユーザーが市場から外部アプリケーションをインストールしたい場合
private void showInstallResultMessage(int msg_id) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setMessage(getText(msg_id));
dialog.setButton(getText(R.string.button_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
dialog.setButton2(getText(R.string.button_install),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=lysesoft.andexplorer"));
startActivity(intent);
finish();
}
});
dialog.show();
}