intentからファイラーアプリ起動でファイル選択
Androidにはファイル選択ダイアリグがない!
そんな訳でLapTimeGPXでは理ろぐの
そんな訳でLapTimeGPXでは理ろぐの
を使わせて貰っていたが
「android7.0でファイル選択ダイアログが開かない」
に遭遇してWeb上をうろついていた時に見つけたのが
「【Android】Intentを発行してファイルパスを取得する」だ!
「android7.0でファイル選択ダイアログが開かない」
に遭遇してWeb上をうろついていた時に見つけたのが
「【Android】Intentを発行してファイルパスを取得する」だ!
これを参考に一部修正してみだ。
下記を発すると左図のように端末内のファイラーアプリがすべて呼び出される。
------------------------------------------------
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, Utility.CHOSE_FILE_CODE);
------------------------------------------------
この中から利用したいファイラーを選択するとそのファイラーが起動する。
------------------------------------------------
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, Utility.CHOSE_FILE_CODE);
------------------------------------------------
この中から利用したいファイラーを選択するとそのファイラーが起動する。
選択したファイルのファイルパスを取得するには下記で受け取る。
-----------------------------------------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == Utility.CHOSE_FILE_CODE && resultCode == RESULT_OK) {
String filePath = data.getDataString();
//storage以降を返す
filePath=filePath.substring(filePath.indexOf("storage"));
String decodedfilePath = URLDecoder.decode(filePath, "utf-8");
Utility.filename=decodedfilePath;
if(Utility.filename.indexOf("laptime.txt")>-1){
Toast.makeText(this, Utility.filename, Toast.LENGTH_SHORT).show();
Utility.dispsave(this,true);
Utility.dispload(this);
Utility.readList(this,true);
}
else Utility.msgToast(this,"No laptime.txt","laptime.txt でない!");
}
} catch (UnsupportedEncodingException e) {}
}
-----------------------------------------------------
このときファイラーによって下記のようにファイパスの先頭赤字部分のような独自のパスが付加されるので、それを取り除く必要がある。
・OIファイルマネージ:
-----------------------------------------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == Utility.CHOSE_FILE_CODE && resultCode == RESULT_OK) {
String filePath = data.getDataString();
//storage以降を返す
filePath=filePath.substring(filePath.indexOf("storage"));
String decodedfilePath = URLDecoder.decode(filePath, "utf-8");
Utility.filename=decodedfilePath;
if(Utility.filename.indexOf("laptime.txt")>-1){
Toast.makeText(this, Utility.filename, Toast.LENGTH_SHORT).show();
Utility.dispsave(this,true);
Utility.dispload(this);
Utility.readList(this,true);
}
else Utility.msgToast(this,"No laptime.txt","laptime.txt でない!");
}
} catch (UnsupportedEncodingException e) {}
}
-----------------------------------------------------
このときファイラーによって下記のようにファイパスの先頭赤字部分のような独自のパスが付加されるので、それを取り除く必要がある。
・OIファイルマネージ:
content://org.openintents.filemanager/storega/emulated/0/laptimegpx.txt
・ファイルマネージャー
・ファイルマネージャー
file:///storega/emulated/0/laptimegpx.txt
・AndroXplorer
・AndroXplorer
file:///storega/emulated/0/laptimegpx.txt
・ESファイルエクスプローラcontent://com.estrongs.files/storega/emulated/0/laptimegpx.txt
ここでは"storage"以降を取得する。
・ESファイルエクスプローラcontent://com.estrongs.files/storega/emulated/0/laptimegpx.txt
ここでは"storage"以降を取得する。
このルールに該当しないファイラーは使えないのだ!
| 固定リンク
「Android/Chrome」カテゴリの記事
- Rimex OS for PCを試す(2016.11.06)
- jarからjavaへ逆コンパイル(2017.04.03)
- 自作androidアプリが暦の2017年問題に・・・(2017.03.09)
- 30日でできる! OS自作入門(2017.02.13)
- スクリーンショットはAndroidアプリ「とりぽ」がいい!(2017.02.12)
コメント