日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android 本地资源 uri,Android 本地文件选择

發布時間:2025/3/15 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 本地资源 uri,Android 本地文件选择 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

打開系統文件:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("*/*");

intent.addCategory(Intent.CATEGORY_OPENABLE);

try {

startActivityForResult(Intent.createChooser(intent, getString(R.string.im_text_select_file)), SEND_NORMAL_FILE);

} catch (android.content.ActivityNotFoundException ex) {

BAUtil.showToast(this, "未找到文件管理應用,請安裝文件管理應用后再試");

}

回調方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case SEND_NORMAL_FILE:

try {

String path = "";

if (Build.VERSION.SDK_INT >= 19) {

path = handleImageOnKitKat(context, data);

} else {

path = handleImageBeforeKitKat(context, data);

}

doSendNormalFile(path, BAAttach.TYPE_FILE);

} catch (Exception e) {

e.printStackTrace();

}

break;

}

}

@TargetApi(Build.VERSION_CODES.KITKAT)

public static String handleImageOnKitKat(Context context, Intent data) {

Uri uri = data.getData();

if (DocumentsContract.isDocumentUri(context, uri)) {

String docId = DocumentsContract.getDocumentId(uri);

if ("com.android.providers.media.documents".equals(uri.getAuthority())) {

String id = docId.split(":")[1];

String selection = MediaStore.Images.Media._ID + "=" + id;

String type = docId.split(":")[0];

Uri contentUri = null;

if (type.equalsIgnoreCase("image")) {

contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

} else if (type.equalsIgnoreCase("audio")) {

contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

} else if (type.equalsIgnoreCase("video")) {

contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

}

return getImagePath(context, contentUri, selection);

} else if ("com.android.providers.media.downloads.documents".equals(uri.getAuthority())) {

Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),

Long.valueOf(docId));

return getImagePath(context, contentUri, null);

} else if ("content".equals(uri.getAuthority())) {

return getImagePath(context, uri, null);

} else if ("file".equals(uri.getAuthority())) {

return uri.getPath();

}

}

return "";

}

public static String handleImageBeforeKitKat(Context context, Intent data) {

Uri uri = data.getData();

return getImagePath(context, uri, null);

}

private static String getImagePath(Context context, Uri uri, String selection) {

String path = null;

Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);

if (cursor != null) {

if (cursor.moveToFirst()) {

path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

}

cursor.close();

}

return path;

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的android 本地资源 uri,Android 本地文件选择的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。