Androidでファイルピッカーダイアログを表示するために、通常のボイラープレートコードを使用しています。ファイル パスを取得したら、aQuery を使用してそのファイルをサーバーにアップロードします。ただし、コードは Android 4.1.2 で動作する私の古いサムスンの携帯電話でのみ動作し、他のデバイスでは動作しません。多くのデバイスを使用してテストしましたが、JellyBean を搭載した Samsung デバイスでのみ動作します。
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(minmeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", minmeType);
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null) {
// it is device with samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, PICKFILE_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.\nPlease download a File Manager and try again.", Toast.LENGTH_LONG).show();
}
onActivityResult コードは次のとおりです。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
fPath = data.getDataString();
File file = new File(fPath.substring(7));
params.put("file" + noFilesAttached, file);
noFilesAttached = noFilesAttached + 1;
path.setText(noFilesAttached + " files attached ");
upload.setText("Upload (" + noFilesAttached + ")");
upload.setVisibility(View.VISIBLE);
isUploaded = false;
}
super.onActivityResult(requestCode, resultCode, data);
}
fPath.substring(7)をfpathに編集しようとしましたが成功しませんでした。サードパーティのライブラリは使用したくありません。
前もって感謝します。