欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 7.0中拍照和圖片裁剪適配的問題詳解

 更新時間:2017年02月08日 11:14:01   作者:LeBron_Six  
這篇文章主要介紹了Android 7.0中拍照和圖片裁剪適配的相關問題,文中通過示例代碼介紹的很詳細,對大家具有一定的參考價值,有需要的朋友們下面來一起學習學習吧。

前言

Android 7.0系統發(fā)布后,拿到能升級的nexus 6P,就開始了7.0的適配。發(fā)現在Android 7.0以上,在相機拍照和圖片裁剪上,可能會碰到以下一些錯誤:

Process: com.yuyh.imgsel, PID: 22995

// 錯誤1
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()

// 錯誤2
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/RxGalleryFinal/IMG_20161018180127.jpg exposed beyond app through Intent.getData()

主要是由于在Android 7.0以后,用了Content Uri 替換了原本的File Uri,故在targetSdkVersion=24的時候,部分 “`Uri.fromFile() “` 方法就不適用了。 **File Uri 與 Content Uri 的區(qū)別** - File Uri 對應的是文件本身的存儲路徑 - Content Uri 對應的是文件在Content Provider的路徑 所以在android 7.0 以上,我們就需要將File Uri轉換為 Content Uri。

具體轉換方法如下:

/**
 * 轉換 content:// uri
 * 
 * @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 crop(String imagePath) {
 File file = new File("xxx.jpg");
 cropImagePath = file.getAbsolutePath();

 Intent intent = new Intent("com.android.camera.action.CROP");
 intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
 intent.putExtra("crop", "true");
 intent.putExtra("aspectX", config.aspectX);
 intent.putExtra("aspectY", config.aspectY);
 intent.putExtra("outputX", config.outputX);
 intent.putExtra("outputY", config.outputY);
 intent.putExtra("scale", true);
 intent.putExtra("return-data", false);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
 intent.putExtra("noFaceDetection", true);
 startActivityForResult(intent, IMAGE_CROP_CODE);
}

這樣就解決了裁剪的問題,但是??!拍照的時候就會出現以下錯誤:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
 tempFile = new File(FileUtils.createRootPath(getActivity()) + "/" + System.currentTimeMillis() + ".jpg");
 LogUtils.e(tempFile.getAbsolutePath());
 FileUtils.createFile(tempFile);
 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
 startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()

這是因為拍照存儲的文件,也需要以Content Uri的形式,故采用以下辦法解決:

Step.1

修改AndroidManifest.xml

<application
 ...>

 <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="{替換為你的包名}.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
   android:name="android.support.FILE_PROVIDER_PATHS"
   android:resource="@xml/provider_paths"/>
 </provider>
</application>

Step.2

在res/xml/下新建provider_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path name="external_files" path="."/>
</paths>

Step.3

修改拍照時的參數

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getActivity(),BuildConfig.APPLICATION_ID + ".provider", tempFile)); //Uri.fromFile(tempFile)

總結

好了,以上就是這篇文章的全部內容了,希望本文的內容對各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流。

相關文章

  • Android開發(fā)實現切換主題及換膚功能示例

    Android開發(fā)實現切換主題及換膚功能示例

    這篇文章主要介紹了Android開發(fā)實現切換主題及換膚功能,涉及Android界面布局與樣式動態(tài)操作相關實現技巧,需要的朋友可以參考下
    2019-03-03
  • android實現手機傳感器調用

    android實現手機傳感器調用

    這篇文章主要為大家詳細介紹了android實現手機傳感器調用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Android ViewPager實現無限循環(huán)效果

    Android ViewPager實現無限循環(huán)效果

    這篇文章主要為大家詳細介紹了Android ViewPager實現無限循環(huán)效果的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Android實現裁剪照片功能

    Android實現裁剪照片功能

    這篇文章主要為大家詳細介紹了Android實現裁剪照片功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android事件沖突解決懸浮窗拖拽處理方案

    Android事件沖突解決懸浮窗拖拽處理方案

    這篇文章主要為大家介紹了Android事件沖突解決懸浮窗拖拽處理方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 官網項目Jetpack?Startup庫學習

    官網項目Jetpack?Startup庫學習

    這篇文章主要為大家介紹了官網項目Jetpack?Startup庫學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Android ScrollView實現下拉彈回動畫效果

    Android ScrollView實現下拉彈回動畫效果

    這篇文章主要為大家詳細介紹了Android ScrollView實現下拉彈回動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 輕松實現Android指南針功能

    輕松實現Android指南針功能

    這篇文章主要介紹了輕松實現Android指南針功能的幾個關鍵步驟,想要實現指南針功能的朋友不要錯過
    2015-12-12
  • Android 活動條ActionBar的詳解及實例代碼

    Android 活動條ActionBar的詳解及實例代碼

    這篇文章主要介紹了Android 活動條ActionBar的詳解及實例代碼的相關資料,需要的朋友可以參考下
    2016-12-12
  • Android 中Crash時如何獲取異常信息詳解及實例

    Android 中Crash時如何獲取異常信息詳解及實例

    這篇文章主要介紹了Android 中Crash時如何獲取異常信息詳解及實例的相關資料,需要的朋友可以參考下
    2017-02-02

最新評論