Android 文件選擇的實(shí)現(xiàn)代碼
打開(kāi)文件選擇器
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) {
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
選擇的結(jié)果
@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();
String path = FileUtils.getPath(this, uri);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
FileUtils文件
public class FileUtils {
public static String getPath(Context context, Uri uri) {
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;
}
}
這個(gè)很簡(jiǎn)單。
出處:http://www.cnblogs.com/linlf03/
相關(guān)文章
Android SwipeMenuListView框架詳解分析
這篇文章主要介紹了Android SwipeMenuListView框架詳解分析的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)手機(jī)震動(dòng)抖動(dòng)效果的方法
今天小編就為大家分享一篇關(guān)于Android實(shí)現(xiàn)手機(jī)震動(dòng)抖動(dòng)效果的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Android自定義ImageView實(shí)現(xiàn)圓角功能
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)圓角功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
如果你在Android Studio碰到gradle的各種問(wèn)題就來(lái)看這篇文章吧(強(qiáng)烈建議收藏)
這篇文章主要介紹了你可能會(huì)在Android Studio碰到gradle的各種問(wèn)題,完美解決關(guān)于gradle的全部問(wèn)題,切記收藏以防需要的時(shí)候找不到了哦2021-08-08
android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果
這篇文章主要為大家詳細(xì)介紹了android控件Banner實(shí)現(xiàn)簡(jiǎn)單輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
解決Android應(yīng)用冷啟動(dòng)時(shí)出現(xiàn)的白屏問(wèn)題的方法
本篇文章主要介紹了解決Android應(yīng)用冷啟動(dòng)時(shí)出現(xiàn)的白屏問(wèn)題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter?Flow實(shí)現(xiàn)滑動(dòng)顯隱層示例詳解
這篇文章主要為大家介紹了Flutter?Flow實(shí)現(xiàn)滑動(dòng)顯隱層示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android開(kāi)發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法示例
這篇文章主要介紹了Android開(kāi)發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法,涉及Android針對(duì)button按鈕的事件響應(yīng)及TextView動(dòng)態(tài)添加相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

