Coder Social home page Coder Social logo

filechoose's Issues

第三方文件管理器Uri转File优化,可兼容迅雷和QQ浏览器,其他的感觉应该类似

改成:

File thirdPartyUriFile = getThirdPartyUriFile(uri);
if (thirdPartyUriFile != null) {
    return thirdPartyUriFile.toString();
}
/**
 * 处理第三方文件管理器的文件Uri
 *
 * @param uri
 *
 * @return
 */
public static File getThirdPartyUriFile(Uri uri) {
    String path = uri.getPath();
    if (!TextUtils.isEmpty(path)) {
        // 去掉第一个'/'
        path = path.substring(1);
        // 去掉新path第一个'/'前面的内容
        path = path.substring(path.indexOf('/'));
        File file = new File(Environment.getExternalStorageDirectory(), path);
        if (file.exists()) {
            return file;
        }
    }
    return null;
}

android11我这样兼容,目前没啥问题

public static String getPath(final Context context, final Uri uri) {
MatchLog.E(RestRequest.TAG, "uri=" + uri);
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
MatchLog.E(RestRequest.TAG, "docId=" + docId);
if (!TextUtils.isEmpty(docId) && docId.startsWith("raw:")) {
return Environment.getExternalStorageDirectory() + "/" + docId.substring(4);
}
final String[] split = docId.split(":");
/final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
/
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
if (!TextUtils.isEmpty(id) && id.startsWith("raw:")) {
return id.substring(4);
}
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} else {
contentUri = MediaStore.Files.getContentUri("external");
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri)) {
String lastPathSegment = uri.getLastPathSegment();
MatchLog.E(RestRequest.TAG, "isGooglePhotosUri=" + lastPathSegment);
return lastPathSegment;
}
if (isQQMediaDocument(uri)) {
String path = uri.getPath();
File fileDir = Environment.getExternalStorageDirectory();
File file = new File(fileDir, path.substring("/QQBrowser".length(), path.length()));
String pathStr = file.exists() ? file.toString() : null;
MatchLog.E(RestRequest.TAG, "isQQMediaDocument=" + pathStr);
return pathStr;
}
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.