Skip to content

Commit

Permalink
添加自定义FileProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Sep 12, 2019
1 parent 59466d8 commit 57570d7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
11 changes: 0 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

<!-- Android 7.0 文件共享配置,必须配置 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>
8 changes: 0 additions & 8 deletions app/src/main/res/xml/file_paths.xml

This file was deleted.

14 changes: 10 additions & 4 deletions imageselector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
package="com.donkingliang.imageselector">

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

android:label="@string/app_name">
<provider
android:name="com.donkingliang.imageselector.provider.ImageSelectorProvider"
android:authorities="${applicationId}.imageSelectorProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/image_selector_file_paths" />
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class ImageSelectorActivity extends AppCompatActivity {

private static final int CAMERA_REQUEST_CODE = 0x00000010;
private Uri mCameraUri;
private String mCameraImagePath;

private boolean isOpenFolder;
private boolean isShowTime;
Expand Down Expand Up @@ -560,9 +561,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
} else if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraUri));
ArrayList<String> images = new ArrayList<>();
images.add(UriUtils.getPathForUri(this, mCameraUri));
if (VersionUtils.isAndroidQ()) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraUri));
images.add(UriUtils.getPathForUri(this, mCameraUri));
} else {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mCameraImagePath))));
images.add(mCameraImagePath);
}
saveImageAndFinish(images, true);
}
}
Expand Down Expand Up @@ -727,9 +733,10 @@ private void openCamera() {
}

if (photoFile != null) {
mCameraImagePath = photoFile.getAbsolutePath();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
//通过FileProvider创建一个content类型的Uri
photoUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", photoFile);
photoUri = FileProvider.getUriForFile(this, getPackageName() + ".imageSelectorProvider", photoFile);
} else {
photoUri = Uri.fromFile(photoFile);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.donkingliang.imageselector.provider;

import android.support.v4.content.FileProvider;

/**
* @Author teach liang
* @Description
* @Date 2019/9/12
*/
public class ImageSelectorProvider extends FileProvider {


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class VersionUtils {
* @return
*/
public static boolean isAndroidQ() {
return true;
// return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
}
}
9 changes: 9 additions & 0 deletions imageselector/src/main/res/xml/image_selector_file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<!-- 这个是保存拍照图片的路径,必须配置。 -->
<external-path
name="images"
path="Pictures" />
</paths>
</resources>

0 comments on commit 57570d7

Please sign in to comment.