第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

正在回答

2 回答

背景:

????在安卓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)文件共享。

修改以下三個文件:
  1. AndroidMainfest.xml為:




  2. <?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>
  3. 在res--->增加xml文件夾---->增加filepaths.xml文件

  4. <?xml?version="1.0"?encoding="utf-8"?>
    <resources>
    ????<paths>
    ????????<external-path
    ????????????name="external_path"
    ????????????path="."?/>
    ????</paths>
    </resources>
  5. 修改MainActivity.java文件為

  6. 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();
    ????????????????????}
    ????????????????}
    
    ????????????}
    ????????}
    ????}
    }
  7. 這們startCamera2就能在真手機(jī)上打開拍照了。


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

哈哈6806406

改了之后,startcamera可以進(jìn)入拍照頁面,但是點擊拍照后調(diào)到原始頁面沒有照片顯示,也找不到存儲地址。
2021-05-28 回復(fù) 有任何疑惑可以回復(fù)我~

背景:

????在安卓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 異常。

解決方案:
?

????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;
????}


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
Android攝像頭基礎(chǔ)
  • 參與學(xué)習(xí)       45007    人
  • 解答問題       140    個

帶你走進(jìn)Android攝像頭的秘密花園,探索Camera的奧秘

進(jìn)入課程

點擊startcamera2之后閃退

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號