課程
/移動開發(fā)
/Android
/Android攝像頭基礎(chǔ)
有大佬告知一下,謝謝
2020-06-08
源自:Android攝像頭基礎(chǔ) 2-3
正在回答
背景:
????在安卓7.0手機(jī)上,調(diào)用系統(tǒng)相機(jī)拍照,結(jié)果出現(xiàn)閃退。
原因:
????Android7.0中為了提高私有文件的安全性,禁止向你的應(yīng)用外公開 file:// URI。 如果一項包含文件 file:// URI類型 的 Intent 離開你的應(yīng)用,應(yīng)用失敗,并出現(xiàn) FileUriExposedException 異常。
解決方案:
FileProvider是Android 7.0出現(xiàn)的新特性,它是ContentProvider的子類,可以通過創(chuàng)建一個Content URI并賦予臨時的文件訪問權(quán)限來代替File URI實現(xiàn)文件共享。
AndroidMainfest.xml為:
<?xml?version="1.0"?encoding="utf-8"?> <manifest?xmlns:android="http://schemas.android.com/apk/res/android" ????package="com.example.androidcamera"> ????<uses-permission?android:name="android.permission.WRITE_EXTERNAL_STORAGE"?/> ????<application ????????android:allowBackup="true" ????????android:icon="@mipmap/ic_launcher" ????????android:label="@string/app_name" ????????android:roundIcon="@mipmap/ic_launcher_round" ????????android:supportsRtl="true" ????????android:theme="@style/AppTheme"> ????????<activity?android:name=".MainActivity"> ????????????<intent-filter> ????????????????<action?android:name="android.intent.action.MAIN"?/> ????????????????<category?android:name="android.intent.category.LAUNCHER"?/> ????????????</intent-filter> ????????????<intent-filter> ????????????????<action?android:name="android.media.action.IMAGE_CAPTURE"?/> ????????????????<category?android:name="android.intent.category.DEFAULT"?/> ????????????</intent-filter> ????????</activity> <!--?android:name="android.support.v4.content.FileProvider" ????? --> ????????<provider ????????????android:authorities="com.example.androidcamera.fileprovider" ????????????android:name="androidx.core.content.FileProvider" ????????????android:grantUriPermissions="true" ????????????android:exported="false"> ????????????<meta-data ????????????????android:name="android.support.FILE_PROVIDER_PATHS" ????????????android:resource="@xml/filepaths"/> ????????</provider> ????</application> </manifest>
在res--->增加xml文件夾---->增加filepaths.xml文件
<?xml?version="1.0"?encoding="utf-8"?> <resources> ????<paths> ????????<external-path ????????????name="external_path" ????????????path="."?/> ????</paths> </resources>
修改MainActivity.java文件為
package?com.example.androidcamera; import?androidx.annotation.Nullable; import?androidx.appcompat.app.AppCompatActivity; import?androidx.core.content.FileProvider; import?android.content.ActivityNotFoundException; import?android.content.ContentValues; import?android.content.Context; import?android.content.Intent; import?android.graphics.Bitmap; import?android.graphics.BitmapFactory; import?android.net.Uri; import?android.os.Build; import?android.os.Bundle; import?android.os.Environment; import?android.provider.MediaStore; import?android.view.View; import?android.widget.ImageView; import?java.io.File; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.IOException; public?class?MainActivity?extends?AppCompatActivity?{ ????private?static?int?REQ_1?=?1; ????private?static?int?REQ_2?=?2; ????private?ImageView?mImageView; ????private?String?mFilePath; ????@Override ????protected?void?onCreate(Bundle?savedInstanceState)?{ ????????super.onCreate(savedInstanceState); ????????setContentView(R.layout.activity_main); ????????mImageView?=?(ImageView)?findViewById(R.id.iv); ????????mFilePath?=?Environment.getExternalStorageDirectory().getPath(); ????????mFilePath?=?mFilePath?+?"/"?+?"temp.png"; ????} ????public?void?startCamera1(View?view){ ????????Intent?intent=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE); ????????startActivityForResult(intent,?REQ_1); ????} ????public?void?startCamera2(View?view){ ????????Intent?intent=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE); //????????Uri?photoUri?=?Uri.fromFile(new?File(mFilePath)); ????????Uri?photoUri?=?getUriForFile(MainActivity.this,new?File(mFilePath)); ????????intent.putExtra(MediaStore.EXTRA_OUTPUT,?photoUri); ????????startActivityForResult(intent,?REQ_2); ????} ????private?static?Uri?getUriForFile(Context?context,?File?file)?{ ????????if?(context?==?null?||?file?==?null)?{ ????????????throw?new?NullPointerException(); ????????} ????????Uri?uri; ????????//判斷是否是AndroidN以及更高的版本 ????????if?(Build.VERSION.SDK_INT?>=?Build.VERSION_CODES.N)?{ ????????????//如果SDK版本>=24,即:Build.VERSION.SDK_INT?>=?24 ????????????//?uri?=?FileProvider.getUriForFile(context,?BuildConfig.APPLICATION_ID?+?".fileProvider",?file); ????????????uri?=?FileProvider.getUriForFile(context,?"com.example.androidcamera.fileprovider",?file); ????????}?else?{ ????????????uri?=?Uri.fromFile(file); ????????} ????????return?uri; ????} ????private?Intent?getInstallIntent()?{ ????????String?savePath?=?"null"; ????????String?appName?=?"AndroidCamera"; ????????String?fileName?=?savePath?+?appName?+?".apk"; ????????Uri?uri?=?null; ????????Intent?intent?=?new?Intent(Intent.ACTION_VIEW); ????????try?{ ????????????if?(Build.VERSION.SDK_INT?>=?24)?{//7.0?Android?N???? ????????????????//com.xxx.xxx.fileprovider為上述manifest中provider所配置相同 ????????????????Context?mContext?=?null; ????????????????uri?=?FileProvider.getUriForFile(mContext,?"com.example.androidcamera.fileprovider",?new?File(fileName)); ????????????????intent.setAction(Intent.ACTION_INSTALL_PACKAGE); ????????????????intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//7.0以后,系統(tǒng)要求授予臨時uri讀取權(quán)限,安裝完畢以后,系統(tǒng)會自動收回權(quán)限,該過程沒有用戶交互 ????????????}?else?{//7.0以下 ????????????????uri?=?Uri.fromFile(new?File(fileName)); ????????????????intent.setAction(Intent.ACTION_VIEW); ????????????????intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ????????????} ????????????intent.setDataAndType(uri,?"application/vnd.android.package-archive"); ????????????startActivity(intent); ????????????return?intent; ????????}?catch?(IllegalArgumentException?e)?{ ????????????e.printStackTrace(); ????????????e.printStackTrace(); ????????}catch?(Exception?e){? ???????????? ????????} ????????return?intent; ????} ????@Override ????protected?void?onActivityResult(int?requestCode,?int?resultCode,?@Nullable?Intent?data)?{ ????????super.onActivityResult(requestCode,?resultCode,?data); ????????if?(resultCode?==?RESULT_OK)?{ ????????????if?(requestCode?==?REQ_1)?{ ????????????????Bundle?bundle?=?data.getExtras(); ????????????????Bitmap?bitmap?=?(Bitmap)?bundle.get("data"); ????????????????mImageView.setImageBitmap(bitmap); ????????????}?else?if?(requestCode?==?REQ_2)?{ ????????????????FileInputStream?fis?=?null; ????????????????try?{ ????????????????????fis?=new?FileInputStream(mFilePath); ????????????????????Bitmap?bitmap?=?BitmapFactory.decodeStream(fis); ????????????????????mImageView.setImageBitmap(bitmap); ????????????????}?catch?(FileNotFoundException?e)?{ ????????????????????e.printStackTrace(); ????????????????}?finally?{ ????????????????????try?{ ????????????????????????fis.close(); ????????????????????}?catch?(IOException?e)?{ ????????????????????????e.printStackTrace(); ????????????????????} ????????????????} ????????????} ????????} ????} }
這們startCamera2就能在真手機(jī)上打開拍照了。
哈哈6806406
解決方案:?
????public?void?openCamera()?{ ????????Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE); ????????imageUri?=?getImageUri(); ????????intent.putExtra(MediaStore.EXTRA_OUTPUT,?imageUri); ????????startActivityForResult(intent,?PHOTO_REQUEST_CAMERA); ????} ? ????public?Uri?getImageUri()?{ ????????File?file?=?new?File(Environment.getExternalStorageDirectory(),?"/temp/"?+?System.currentTimeMillis()?+?".jpg"); ????????if?(!file.getParentFile().exists())?{ ????????????file.getParentFile().mkdirs(); ????????} ????????String?path?=?file.getPath(); ????????if?(Build.VERSION.SDK_INT?<?Build.VERSION_CODES.N)?{ ????????????imageUri?=?Uri.fromFile(file); ????????}?else?{ ????????????//兼容android7.0?使用共享文件的形式 ????????????ContentValues?contentValues?=?new?ContentValues(1); ????????????contentValues.put(MediaStore.Images.Media.DATA,?path); ????????????imageUri?=?this.getApplication().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,?contentValues); ????????} ????????return?imageUri; ????}
舉報
帶你走進(jìn)Android攝像頭的秘密花園,探索Camera的奧秘
1 回答startcamera2閃退
2 回答拍完照之后閃退
1 回答拍完照片點對號之后就閃退
2 回答閃退怎么辦
1 回答自定義相機(jī)閃退??
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2020-06-20
背景:
????在安卓7.0手機(jī)上,調(diào)用系統(tǒng)相機(jī)拍照,結(jié)果出現(xiàn)閃退。
原因:
????Android7.0中為了提高私有文件的安全性,禁止向你的應(yīng)用外公開 file:// URI。 如果一項包含文件 file:// URI類型 的 Intent 離開你的應(yīng)用,應(yīng)用失敗,并出現(xiàn) FileUriExposedException 異常。
解決方案:
FileProvider是Android 7.0出現(xiàn)的新特性,它是ContentProvider的子類,可以通過創(chuàng)建一個Content URI并賦予臨時的文件訪問權(quán)限來代替File URI實現(xiàn)文件共享。
修改以下三個文件:
AndroidMainfest.xml為:
在res--->增加xml文件夾---->增加filepaths.xml文件
修改MainActivity.java文件為
這們startCamera2就能在真手機(jī)上打開拍照了。
2020-06-19
背景:
????在安卓7.0手機(jī)上,調(diào)用系統(tǒng)相機(jī)拍照,結(jié)果出現(xiàn)閃退。
原因:
????Android7.0中為了提高私有文件的安全性,禁止向你的應(yīng)用外公開 file:// URI。 如果一項包含文件 file:// URI類型 的 Intent 離開你的應(yīng)用,應(yīng)用失敗,并出現(xiàn) FileUriExposedException 異常。
解決方案:
?