寫在前面
這個sdk在前一段時間就完成了音比,今天又拿來修改了一下妖滔,徹底把權(quán)限問題多解決了隧哮。經(jīng)過這次寫這個sdk,深深的發(fā)現(xiàn)安卓在一次又一次升級用戶體驗座舍,但是沮翔,但是很是折磨程序員啊。一開始我以為這是一個很簡單的事曲秉,結(jié)果被各種權(quán)限版本設(shè)置搞得都要奔潰了采蚀。
開始
其實網(wǎng)上的文章都很詳細比如文章1(http://www.voidcn.com/article/p-ehdfbrsl-nh.html )文章2(http://www.voidcn.com/article/p-ehdfbrsl-nh.html )。 其實實現(xiàn)過程都大同小異承二,我們主要來看我遇到得的問題榆鼠,當然這些問題都是在6.0以上的版本出現(xiàn)的問題,之前的都沒有亥鸠。
安卓6.0以上獲取攝像機權(quán)限
在最開始我們在AndroidManifest.xml文件中添加獲取相機權(quán)限妆够,如下
<uses-permission android:name="android.permission.CAMERA"/>
然后運行程序第一次并沒有提示我獲得相機權(quán)限,然后再點擊打開相機閃退负蚊,媽耶神妹。然后就查資料說,相機這類權(quán)限屬于危險級家妆,需要動態(tài)請求鸵荠。所以這里就開始編寫動態(tài)請求,網(wǎng)上蠻多文章說這個的伤极,大多都需要使用其他什么插件得的腰鬼。官方給出的代碼:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
更多詳細的可以看看官網(wǎng)文檔(https://developer.android.com/training/permissions/requesting)。 這里用的ActivityCompat是使用的Android.support.v4的架包塑荒。所以導(dǎo)入unity后我們還需要導(dǎo)入這個架包不然的話會報錯熄赡。看有文章說這個架包在安卓SDK的目錄下可以找到齿税,目錄: AndroidSDK/extras/android/m2repository/com/android/support/support-v4
里面有很多版本的彼硫,移到unity中,沒有用凌箕,還是報錯E±骸!牵舱!可能是我用的版本不對串绩。這里我使用的是之前接GooglePlay的SDK里面的架包。加進去了沒有報錯了芜壁。但是·······
點擊打開相機出來了礁凡,選擇權(quán)限框高氮,但是是閃現(xiàn)的。對就是閃現(xiàn)的顷牌。
沒辦法剪芍,查了很久,然后看unity官方文檔(https://docs.unity3d.com/Manual/android-manifest.html ) 中說的是需要再加一個設(shè)置到AndroidManifest里窟蓝,加在Application之間或者Activity即可罪裹。
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
因為unity本身是不支持動態(tài)權(quán)限系統(tǒng)。同時我又看到一個玄學答案运挫,在你的代碼里加入這個:
/// <summary>
/// 無用方法状共,用來打開攝像頭權(quán)限
/// </summary>
/// <returns></returns>
private IEnumerator OpenCameraPermisson()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) yield break;
WebCamDevice[] devices = WebCamTexture.devices;
}
這兩個是我一起加的好像都有效果,調(diào)用webcam時unity會自動提示你打開相機權(quán)限谁帕。就這樣解決了問題口芍。
在動態(tài)獲取權(quán)限那個地方修改了一下寫法,不太想使用其他的架包啥的雇卷,我的寫法是:
//先檢測權(quán)限
public void OpenTakePhoto() {
if (Build.VERSION.SDK_INT > 22) {
if (this.checkSelfPermission(android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//先判斷有沒有權(quán)限 鬓椭,沒有就在這里進行權(quán)限的申請
this.requestPermissions(
new String[]{android.Manifest.permission.CAMERA}, CAMERA_OK);
} else {
TakePhoto();
}
} else {
TakePhoto();
}
}
簡單點的說就是把ActivityCompat替換成當前的Activity就可以。這樣就可以不使用support.v4架包关划。
相機打開閃退
對的小染,你沒看錯。不過這個很快就解決了贮折。這個問題出現(xiàn)的原因主要是由于在Android 7.0以后裤翩,用了Content Uri 替換了原本的File Uri,故在targetSdkVersion=24的時候调榄,部分 “Uri.fromFile()“
方法就不適用了踊赠。 File Uri 與 Content Uri 的區(qū)別 - File Uri 對應(yīng)的是文件本身的存儲路徑 - Content Uri 對應(yīng)的是文件在Content Provider的路徑 所以在android 7.0 以上,我們就需要將File Uri轉(zhuǎn)換為 Content Uri每庆。這里寫一個轉(zhuǎn)化的方法:
/**
* 轉(zhuǎn)換 content:// uri
* 7.0以后使用
*
* @param imageFile
* @return
*/
public Uri getImageContentUri(File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
然后再打開相機的地方做一個選擇:
//打開相機
private void TakePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
File outFile = new File(UnityUsePicturePath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageContentUri(outFile));
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(UnityUsePicturePath)));
}
startActivityForResult(intent, PHOTOHRAPH);
}
存儲權(quán)限問題
這里我們需要添加存儲的權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我們可以在打開相冊的時候檢測一下筐带,動態(tài)請求一下。
代碼
我把java代碼寫成了Activity類缤灵,然后我們直接在unity中的調(diào)用傳入?yún)?shù)即可:
//獲得照片
public void GetPhoto(GetPhotoType photoType, bool isCutPicture = false)
{
var strType = Enum.GetName(typeof(GetPhotoType), photoType);
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent", unityActivity, gallerySdk);
intentObject.Call<AndroidJavaObject>("putExtra", "type", strType);
intentObject.Call<AndroidJavaObject>("putExtra", "UnityPersistentDataPath", Application.persistentDataPath);
intentObject.Call<AndroidJavaObject>("putExtra", "isCutPicture", isCutPicture);
unityActivity.Call("startActivity", intentObject);
Debug.Log("GetPhoto Android startActivity");
}
這樣我們就可以不需要重新寫AndroidManifest了伦籍,只需要在其中申明一下Activity即可:
<!-- 打開相冊及相機 設(shè)置為透明 -->
<!-- android:screenOrientation="portrait" android:configChanges="orientation|screenSize|keyboardHidden|keyboard|"解決相機Activity無法關(guān)閉問題 -->
<activity android:name="com.unity.gallerylibrary.GalleryManager" android:screenOrientation="portrait" android:configChanges="orientation|screenSize|keyboardHidden|keyboard|">
<!-- 解決這個問題 Only fullscreen opaque activities can request orientation -->
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowDisablePreview">true</item>
</activity>
其他的看一下源碼就可以了。很簡單腮出。
示例代碼
void OnGUI()
{
if (GUILayout.Button("Take Photo", GUILayout.Width(200), GUILayout.Height(200)))
{
GalleryManager.Instance.GetPhoto(GetPhotoType.Carmera, (texture) => { rawImage.texture = texture; });
}
if (GUILayout.Button("Open Gallery", GUILayout.Width(200), GUILayout.Height(200)))
{
GalleryManager.Instance.GetPhoto(GetPhotoType.Gallery, (texture) => { rawImage.texture = texture; });
}
if (GUILayout.Button("Show Image", GUILayout.Width(200), GUILayout.Height(200)))
{
StartCoroutine(GetImageByPath(Application.persistentDataPath + "/UNITY_GALLERY_PICTUER.png"));
}
}
效果圖
參考文章:
http://www.voidcn.com/article/p-ehdfbrsl-nh.html
https://www.xuanyusong.com/archives/1480
https://blog.csdn.net/dengmengxin/article/details/38702467
權(quán)限:
http://www.reibang.com/p/765603bebced
https://blog.csdn.net/beijinghsj/article/details/53581764
https://docs.unity3d.com/Manual/android-manifest.html
https://blog.csdn.net/haojiagou/article/details/80761709