├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── modules.xml ├── runConfigurations.xml ├── compiler.xml ├── gradle.xml └── misc.xml ├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable │ │ │ │ ├── actionsheet_top_normal.9.png │ │ │ │ ├── actionsheet_top_pressed.9.png │ │ │ │ ├── actionsheet_bottom_normal.9.png │ │ │ │ ├── actionsheet_bottom_pressed.9.png │ │ │ │ ├── actionsheet_middle_normal.9.png │ │ │ │ ├── actionsheet_middle_pressed.9.png │ │ │ │ ├── actionsheet_single_normal.9.png │ │ │ │ ├── actionsheet_single_pressed.9.png │ │ │ │ ├── actionsheet_top_selector.xml │ │ │ │ ├── actionsheet_bottom_selector.xml │ │ │ │ ├── actionsheet_middle_selector.xml │ │ │ │ └── actionsheet_single_selector.xml │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── anim │ │ │ │ ├── actionsheet_dialog_in.xml │ │ │ │ └── actionsheet_dialog_out.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ ├── layout │ │ │ │ ├── activity_main.xml │ │ │ │ └── view_actionsheet.xml │ │ │ └── xml │ │ │ │ └── file_provider.xml │ │ ├── assets │ │ │ └── uploadImgForH5.html │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── xf │ │ │ └── uploadh5demo │ │ │ ├── utils │ │ │ └── GetPathFromUri4kitkat.java │ │ │ ├── view │ │ │ └── ActionSheetDialog.java │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── xf │ │ │ └── uploadh5demo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── xf │ │ └── uploadh5demo │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── screenshot └── 1.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── README.md ├── .gitignore ├── gradle.properties ├── gradlew.bat └── gradlew /.idea/.name: -------------------------------------------------------------------------------- 1 | UploadH5Demo -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /screenshot/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/screenshot/1.gif -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_top_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_top_normal.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_top_pressed.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_top_pressed.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_bottom_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_bottom_normal.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_bottom_pressed.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_bottom_pressed.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_middle_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_middle_normal.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_middle_pressed.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_middle_pressed.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_single_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_single_normal.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_single_pressed.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xufei5789651/UploadH5Demo/HEAD/app/src/main/res/drawable/actionsheet_single_pressed.9.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | UploadH5Demo 3 | com.xf.uploadh5demo.fileprovider 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UploadH5Demo 2 | Android项目中H5页面调用相机、相册 3 | 4 | 经过真机测试,可以H5页面可以调起Android系统的相机、相册 5 | 6 | 效果图;(图为模拟器上的效果) 7 | 8 | ![image](https://github.com/xufei5789651/UploadH5Demo/blob/master/screenshot/1.gif) 9 | -------------------------------------------------------------------------------- /app/src/main/res/anim/actionsheet_dialog_in.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/anim/actionsheet_dialog_out.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_top_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_bottom_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_middle_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/actionsheet_single_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/test/java/com/xf/uploadh5demo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.xf.uploadh5demo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/xf/uploadh5demo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.xf.uploadh5demo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/assets/uploadImgForH5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | H5页面上传手机照片 10 | 11 | 12 |
13 |

图片1:

14 |

图片2:

15 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #00000000 7 | #000000 8 | #c6c6c6 9 | #037BFF 10 | #FD4A2E 11 | #8F8F8F 12 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.xf.uploadh5demo" 9 | minSdkVersion 16 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.4.0' 26 | } 27 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | *.iml 3 | .gradle 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | ======= 11 | # Built application files 12 | *.apk 13 | *.ap_ 14 | 15 | # Files for the ART/Dalvik VM 16 | *.dex 17 | 18 | # Java class files 19 | *.class 20 | 21 | # Generated files 22 | bin/ 23 | gen/ 24 | out/ 25 | 26 | # Gradle files 27 | .gradle/ 28 | build/ 29 | 30 | # Local configuration file (sdk path, etc) 31 | local.properties 32 | 33 | # Proguard folder generated by Eclipse 34 | proguard/ 35 | 36 | # Log Files 37 | *.log 38 | 39 | # Android Studio Navigation editor temp files 40 | .navigation/ 41 | 42 | # Android Studio captures folder 43 | captures/ 44 | 45 | # Intellij 46 | *.iml 47 | .idea/workspace.xml 48 | 49 | # Keystore files 50 | *.jks 51 | >>>>>>> ddcaffba4786af12fc908ba4a26a75b670a178f4 52 | -------------------------------------------------------------------------------- /app/src/main/res/xml/file_provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 11 | 12 | 15 | 16 | 19 | 20 | 23 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 32 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 29 | 30 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_actionsheet.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 22 | 23 | 29 | 30 | 35 | 36 | 37 | 38 | 49 | 50 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 23 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 50 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/xf/uploadh5demo/utils/GetPathFromUri4kitkat.java: -------------------------------------------------------------------------------- 1 | package com.xf.uploadh5demo.utils; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.ContentUris; 5 | import android.content.Context; 6 | import android.database.Cursor; 7 | import android.net.Uri; 8 | import android.os.Build; 9 | import android.os.Environment; 10 | import android.provider.DocumentsContract; 11 | import android.provider.MediaStore; 12 | 13 | public class GetPathFromUri4kitkat { 14 | 15 | /** 16 | * 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使 17 | */ 18 | @SuppressLint("NewApi") 19 | public static String getPath(final Context context, final Uri uri) { 20 | 21 | final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 22 | 23 | // DocumentProvider 24 | if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 25 | // ExternalStorageProvider 26 | if (isExternalStorageDocument(uri)) { 27 | final String docId = DocumentsContract.getDocumentId(uri); 28 | final String[] split = docId.split(":"); 29 | final String type = split[0]; 30 | 31 | if ("primary".equalsIgnoreCase(type)) { 32 | return Environment.getExternalStorageDirectory() + "/" + split[1]; 33 | } 34 | 35 | // TODO handle non-primary volumes 36 | } 37 | // DownloadsProvider 38 | else if (isDownloadsDocument(uri)) { 39 | 40 | final String id = DocumentsContract.getDocumentId(uri); 41 | final Uri contentUri = ContentUris.withAppendedId( 42 | Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 43 | 44 | return getDataColumn(context, contentUri, null, null); 45 | } 46 | // MediaProvider 47 | else if (isMediaDocument(uri)) { 48 | final String docId = DocumentsContract.getDocumentId(uri); 49 | final String[] split = docId.split(":"); 50 | final String type = split[0]; 51 | 52 | Uri contentUri = null; 53 | if ("image".equals(type)) { 54 | contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 55 | } else if ("video".equals(type)) { 56 | contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 57 | } else if ("audio".equals(type)) { 58 | contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 59 | } 60 | 61 | final String selection = "_id=?"; 62 | final String[] selectionArgs = new String[] { split[1] }; 63 | 64 | return getDataColumn(context, contentUri, selection, selectionArgs); 65 | } 66 | } 67 | // MediaStore (and general) 68 | else if ("content".equalsIgnoreCase(uri.getScheme())) { 69 | return getDataColumn(context, uri, null, null); 70 | } 71 | // File 72 | else if ("file".equalsIgnoreCase(uri.getScheme())) { 73 | return uri.getPath(); 74 | } 75 | 76 | return null; 77 | } 78 | 79 | /** 80 | * Get the value of the data column for this Uri. This is useful for 81 | * MediaStore Uris, and other file-based ContentProviders. 82 | * 83 | * @param context 84 | * The context. 85 | * @param uri 86 | * The Uri to query. 87 | * @param selection 88 | * (Optional) Filter used in the query. 89 | * @param selectionArgs 90 | * (Optional) Selection arguments used in the query. 91 | * @return The value of the _data column, which is typically a file path. 92 | */ 93 | public static String getDataColumn(Context context, Uri uri, String selection, 94 | String[] selectionArgs) { 95 | 96 | Cursor cursor = null; 97 | final String column = "_data"; 98 | final String[] projection = { column }; 99 | 100 | try { 101 | cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 102 | null); 103 | if (cursor != null && cursor.moveToFirst()) { 104 | final int column_index = cursor.getColumnIndexOrThrow(column); 105 | return cursor.getString(column_index); 106 | } 107 | } finally { 108 | if (cursor != null) 109 | cursor.close(); 110 | } 111 | return null; 112 | } 113 | 114 | /** 115 | * @param uri 116 | * The Uri to check. 117 | * @return Whether the Uri authority is ExternalStorageProvider. 118 | */ 119 | public static boolean isExternalStorageDocument(Uri uri) { 120 | return "com.android.externalstorage.documents".equals(uri.getAuthority()); 121 | } 122 | 123 | /** 124 | * @param uri 125 | * The Uri to check. 126 | * @return Whether the Uri authority is DownloadsProvider. 127 | */ 128 | public static boolean isDownloadsDocument(Uri uri) { 129 | return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 130 | } 131 | 132 | /** 133 | * @param uri 134 | * The Uri to check. 135 | * @return Whether the Uri authority is MediaProvider. 136 | */ 137 | public static boolean isMediaDocument(Uri uri) { 138 | return "com.android.providers.media.documents".equals(uri.getAuthority()); 139 | } 140 | } -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/xf/uploadh5demo/view/ActionSheetDialog.java: -------------------------------------------------------------------------------- 1 | package com.xf.uploadh5demo.view; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | import android.app.Dialog; 8 | import android.content.Context; 9 | import android.graphics.Color; 10 | import android.view.Display; 11 | import android.view.Gravity; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.View.OnClickListener; 15 | import android.view.Window; 16 | import android.view.WindowManager; 17 | import android.widget.LinearLayout; 18 | import android.widget.LinearLayout.LayoutParams; 19 | import android.widget.ScrollView; 20 | import android.widget.TextView; 21 | 22 | import com.xf.uploadh5demo.R; 23 | 24 | public class ActionSheetDialog { 25 | private Context context; 26 | private Dialog dialog; 27 | private TextView txt_title; 28 | private TextView txt_cancel; 29 | private LinearLayout lLayout_content; 30 | private ScrollView sLayout_content; 31 | private boolean showTitle = false; 32 | private List sheetItemList; 33 | private Display display; 34 | private OnClickListener onClickListener; 35 | 36 | public void setOnClickListener(OnClickListener onClickListener) { 37 | this.onClickListener = onClickListener; 38 | } 39 | 40 | public ActionSheetDialog(Context context) { 41 | this.context = context; 42 | WindowManager windowManager = (WindowManager) context 43 | .getSystemService(Context.WINDOW_SERVICE); 44 | display = windowManager.getDefaultDisplay(); 45 | } 46 | 47 | public ActionSheetDialog builder() { 48 | // ��ȡDialog���� 49 | View view = LayoutInflater.from(context).inflate( 50 | R.layout.view_actionsheet, null); 51 | 52 | // ����Dialog��С���Ϊ��Ļ��� 53 | view.setMinimumWidth(display.getWidth()); 54 | 55 | // ��ȡ�Զ���Dialog�����еĿؼ� 56 | sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content); 57 | lLayout_content = (LinearLayout) view 58 | .findViewById(R.id.lLayout_content); 59 | txt_title = (TextView) view.findViewById(R.id.txt_title); 60 | txt_cancel = (TextView) view.findViewById(R.id.txt_cancel); 61 | txt_cancel.setOnClickListener(new OnClickListener() { 62 | @Override 63 | public void onClick(View v) { 64 | dialog.dismiss(); 65 | if (onClickListener!=null) 66 | onClickListener.onClick(v); 67 | } 68 | }); 69 | 70 | // ����Dialog���ֺͲ��� 71 | dialog = new Dialog(context, R.style.ActionSheetDialogStyle); 72 | dialog.setContentView(view); 73 | Window dialogWindow = dialog.getWindow(); 74 | dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM); 75 | WindowManager.LayoutParams lp = dialogWindow.getAttributes(); 76 | lp.x = 0; 77 | lp.y = 0; 78 | dialogWindow.setAttributes(lp); 79 | 80 | return this; 81 | } 82 | 83 | public ActionSheetDialog setTitle(String title) { 84 | showTitle = true; 85 | txt_title.setVisibility(View.VISIBLE); 86 | txt_title.setText(title); 87 | return this; 88 | } 89 | 90 | public ActionSheetDialog setCancelable(boolean cancel) { 91 | dialog.setCancelable(cancel); 92 | return this; 93 | } 94 | 95 | public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) { 96 | dialog.setCanceledOnTouchOutside(cancel); 97 | return this; 98 | } 99 | 100 | /** 101 | * 102 | * @param strItem 103 | * ��Ŀ��� 104 | * @param color 105 | * ��Ŀ������ɫ������null��Ĭ����ɫ 106 | * @param listener 107 | * @return 108 | */ 109 | public ActionSheetDialog addSheetItem(String strItem, SheetItemColor color, 110 | OnSheetItemClickListener listener) { 111 | if (sheetItemList == null) { 112 | sheetItemList = new ArrayList(); 113 | } 114 | sheetItemList.add(new SheetItem(strItem, color, listener)); 115 | return this; 116 | } 117 | 118 | /** ������Ŀ���� */ 119 | private void setSheetItems() { 120 | if (sheetItemList == null || sheetItemList.size() <= 0) { 121 | return; 122 | } 123 | 124 | int size = sheetItemList.size(); 125 | 126 | // TODO �߶ȿ��ƣ�����ѽ���취 127 | // �����Ŀ����ʱ����Ƹ߶� 128 | if (size >= 7) { 129 | LayoutParams params = (LayoutParams) sLayout_content 130 | .getLayoutParams(); 131 | params.height = display.getHeight() / 2; 132 | sLayout_content.setLayoutParams(params); 133 | } 134 | 135 | // ѭ�������Ŀ 136 | for (int i = 1; i <= size; i++) { 137 | final int index = i; 138 | SheetItem sheetItem = sheetItemList.get(i - 1); 139 | String strItem = sheetItem.name; 140 | SheetItemColor color = sheetItem.color; 141 | final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener; 142 | 143 | TextView textView = new TextView(context); 144 | textView.setText(strItem); 145 | textView.setTextSize(18); 146 | textView.setGravity(Gravity.CENTER); 147 | 148 | // ����ͼƬ 149 | if (size == 1) { 150 | if (showTitle) { 151 | textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); 152 | } else { 153 | textView.setBackgroundResource(R.drawable.actionsheet_single_selector); 154 | } 155 | } else { 156 | if (showTitle) { 157 | if (i >= 1 && i < size) { 158 | textView.setBackgroundResource(R.drawable.actionsheet_middle_selector); 159 | } else { 160 | textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); 161 | } 162 | } else { 163 | if (i == 1) { 164 | textView.setBackgroundResource(R.drawable.actionsheet_top_selector); 165 | } else if (i < size) { 166 | textView.setBackgroundResource(R.drawable.actionsheet_middle_selector); 167 | } else { 168 | textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); 169 | } 170 | } 171 | } 172 | 173 | // ������ɫ 174 | if (color == null) { 175 | textView.setTextColor(Color.parseColor(SheetItemColor.Blue 176 | .getName())); 177 | } else { 178 | textView.setTextColor(Color.parseColor(color.getName())); 179 | } 180 | 181 | // �߶� 182 | float scale = context.getResources().getDisplayMetrics().density; 183 | int height = (int) (45 * scale + 0.5f); 184 | textView.setLayoutParams(new LayoutParams( 185 | LayoutParams.MATCH_PARENT, height)); 186 | 187 | // ����¼� 188 | textView.setOnClickListener(new OnClickListener() { 189 | @Override 190 | public void onClick(View v) { 191 | listener.onClick(index); 192 | dialog.dismiss(); 193 | } 194 | }); 195 | 196 | lLayout_content.addView(textView); 197 | } 198 | } 199 | 200 | public void show() { 201 | setSheetItems(); 202 | dialog.show(); 203 | } 204 | 205 | public interface OnSheetItemClickListener { 206 | void onClick(int which); 207 | } 208 | 209 | public class SheetItem { 210 | String name; 211 | OnSheetItemClickListener itemClickListener; 212 | SheetItemColor color; 213 | 214 | public SheetItem(String name, SheetItemColor color, 215 | OnSheetItemClickListener itemClickListener) { 216 | this.name = name; 217 | this.color = color; 218 | this.itemClickListener = itemClickListener; 219 | } 220 | } 221 | 222 | public enum SheetItemColor { 223 | Blue("#037BFF"), Red("#FD4A2E"); 224 | 225 | private String name; 226 | 227 | private SheetItemColor(String name) { 228 | this.name = name; 229 | } 230 | 231 | public String getName() { 232 | return name; 233 | } 234 | 235 | public void setName(String name) { 236 | this.name = name; 237 | } 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /app/src/main/java/com/xf/uploadh5demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xf.uploadh5demo; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | import android.net.Uri; 7 | import android.os.Build; 8 | import android.os.Environment; 9 | import android.provider.MediaStore; 10 | import android.support.annotation.NonNull; 11 | import android.support.v4.app.ActivityCompat; 12 | import android.support.v7.app.AppCompatActivity; 13 | import android.os.Bundle; 14 | import android.util.Log; 15 | import android.view.View; 16 | import android.webkit.ValueCallback; 17 | import android.webkit.WebChromeClient; 18 | import android.webkit.WebView; 19 | import android.widget.Toast; 20 | 21 | import com.xf.uploadh5demo.utils.GetPathFromUri4kitkat; 22 | import com.xf.uploadh5demo.view.ActionSheetDialog; 23 | 24 | import java.io.File; 25 | 26 | import static android.support.v4.content.FileProvider.getUriForFile; 27 | 28 | public class MainActivity extends AppCompatActivity { 29 | 30 | private static final String TAG = MainActivity.class.getSimpleName(); 31 | private static final int REQUEST_CODE_TAKE_PICETURE = 11; 32 | private static final int REQUEST_CODE_PICK_PHOTO = 12; 33 | private static final int REQUEST_CODE_PERMISSION = 13; 34 | public static ValueCallback mFilePathCallback; 35 | private File picturefile; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_main); 41 | initView(); 42 | initPermissionForCamera(); 43 | } 44 | 45 | private void initPermissionForCamera() { 46 | int flag = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA); 47 | if (PackageManager.PERMISSION_GRANTED != flag) { 48 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_PERMISSION); 49 | } 50 | } 51 | 52 | @Override 53 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 54 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 55 | if (REQUEST_CODE_PERMISSION == requestCode) { 56 | switch (grantResults[0]) { 57 | case PackageManager.PERMISSION_DENIED: 58 | boolean isSecondRequest = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA); 59 | if (isSecondRequest) 60 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_PERMISSION); 61 | else 62 | Toast.makeText(this, "拍照权限被禁用,请在权限管理修改", Toast.LENGTH_SHORT).show(); 63 | break; 64 | } 65 | } 66 | } 67 | 68 | private void initView() { 69 | WebView webview = (WebView) findViewById(R.id.webview); 70 | webview.loadUrl("file:///android_asset/uploadImgForH5.html"); 71 | webview.setWebChromeClient(new WebChromeClient() { 72 | 73 | @Override 74 | public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {//5.0+ 75 | Log.e(TAG, "--------调用onShowFileChooser"); 76 | showDialog(); 77 | mFilePathCallback = filePathCallback; 78 | return true; 79 | } 80 | 81 | //openFileChooser 方法是隐藏方法 82 | public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {// android 系统版本>4.1.1 83 | Log.e(TAG, "--------调用openFileChooser"); 84 | showDialog(); 85 | mFilePathCallback = uploadMsg; 86 | } 87 | 88 | public void openFileChooser(ValueCallback uploadMsg) {//android 系统版本<3.0 89 | showDialog(); 90 | mFilePathCallback = uploadMsg; 91 | } 92 | 93 | public void openFileChooser(ValueCallback uploadMsg, String acceptType) {//android 系统版本3.0+ 94 | showDialog(); 95 | mFilePathCallback = uploadMsg; 96 | } 97 | 98 | }); 99 | 100 | 101 | } 102 | 103 | private void showDialog() { 104 | ActionSheetDialog dialog = new ActionSheetDialog(MainActivity.this).builder().addSheetItem("拍照", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { 105 | @Override 106 | public void onClick(int which) { 107 | takeForPicture(); 108 | } 109 | }).addSheetItem("相册", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { 110 | @Override 111 | public void onClick(int which) { 112 | takeForPhoto(); 113 | } 114 | }).setCancelable(false).setCanceledOnTouchOutside(false); 115 | 116 | dialog.show(); 117 | //设置点击“取消”按钮监听,目的取消mFilePathCallback回调,可以重复调起弹窗 118 | dialog.setOnClickListener(new View.OnClickListener() { 119 | @Override 120 | public void onClick(View view) { 121 | cancelFilePathCallback(); 122 | } 123 | }); 124 | } 125 | 126 | /** 127 | * 调用相册 128 | */ 129 | private void takeForPhoto() { 130 | Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 131 | startActivityForResult(intent, REQUEST_CODE_PICK_PHOTO); 132 | 133 | } 134 | 135 | /** 136 | * 调用相机 137 | */ 138 | private void takeForPicture() { 139 | // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);//手机图片的公共目录 140 | File pFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyPictures");//图片位置 141 | if (!pFile.exists()) { 142 | pFile.mkdirs(); 143 | } 144 | //拍照所存路径 145 | picturefile = new File(pFile + File.separator + "IvMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 146 | Log.e(TAG, "拍照所存路径: ===" + picturefile.getAbsolutePath()); 147 | 148 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 149 | if (Build.VERSION.SDK_INT > 23) {//7.0及以上 150 | Uri contentUri = getUriForFile(MainActivity.this, getResources().getString(R.string.filepath), picturefile); 151 | grantUriPermission(getPackageName(),contentUri,Intent.FLAG_GRANT_READ_URI_PERMISSION); 152 | intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); 153 | } else {//7.0以下 154 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picturefile)); 155 | } 156 | startActivityForResult(intent, REQUEST_CODE_TAKE_PICETURE); 157 | 158 | } 159 | 160 | private void cancelFilePathCallback() { 161 | if (mFilePathCallback != null) { 162 | mFilePathCallback.onReceiveValue(null); 163 | mFilePathCallback = null; 164 | } 165 | } 166 | 167 | 168 | @Override 169 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 170 | super.onActivityResult(requestCode, resultCode, data); 171 | switch (requestCode) { 172 | case REQUEST_CODE_TAKE_PICETURE: 173 | takePictureResult(resultCode); 174 | break; 175 | 176 | case REQUEST_CODE_PICK_PHOTO: 177 | takePhotoResult(resultCode, data); 178 | 179 | break; 180 | } 181 | } 182 | 183 | private void takePhotoResult(int resultCode, Intent data) { 184 | if (mFilePathCallback != null){ 185 | Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); 186 | if (result != null) { 187 | String path = GetPathFromUri4kitkat.getPath(this, result); 188 | Uri uri = Uri.fromFile(new File(path)); 189 | if (Build.VERSION.SDK_INT > 18) { 190 | mFilePathCallback.onReceiveValue(new Uri[]{uri}); 191 | } else { 192 | mFilePathCallback.onReceiveValue(uri); 193 | } 194 | 195 | }else { 196 | mFilePathCallback.onReceiveValue(null); 197 | mFilePathCallback = null; 198 | } 199 | } 200 | } 201 | 202 | private void takePictureResult(int resultCode) { 203 | if (mFilePathCallback != null) { 204 | if (resultCode == RESULT_OK) { 205 | Uri uri = Uri.fromFile(picturefile); 206 | if (Build.VERSION.SDK_INT > 18) { 207 | mFilePathCallback.onReceiveValue(new Uri[]{uri}); 208 | } else { 209 | mFilePathCallback.onReceiveValue(uri); 210 | } 211 | } else { 212 | //点击了file按钮,必须有一个返回值,否则会卡死 213 | mFilePathCallback.onReceiveValue(null); 214 | mFilePathCallback = null; 215 | } 216 | } 217 | 218 | } 219 | 220 | 221 | 222 | } 223 | --------------------------------------------------------------------------------