… outputImage.createNewFile();//如果创建文件失败,则抛出异常 … //getExternalCacheDir()是当前应用缓存数据(应用关联缓存目录)的位置,这样写的好处是因为从Android6.0开始,读写SD卡被列为危险项目,处理起来比较复杂,而采用应用关联缓存目录可以绕过这一步。
//这里获取URI时要进行判断,如果是安卓7.0以上的话,就要使用一个特殊的内容提供器FileProvider,它可以选择性地将封装过的Uri提供给外部
① 进入AndroidManifest.xml中,如下改动
<application … <provider android:authorities="com.wcs.choosepictest.fileprovider" android:name="android.support.v4.content.FileProvider"//这里是固定的写法 android:exported="false"//这里必须为false,为true则会报安全异常 android:grantUripermissions="true">//表示授予 URI 临时访问权限。 <meta-data //指定Uri的共享路径 android:name="android.support.FILE_PROVIDER_PATHS"//这里是固定的写法 android:resource="@xml/file_paths"/> </provider> …</application>② 新建xml目录,并将内容设为:
<?xml version="1.0" encoding="utf-8"?><path xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path=""/></path>附源代码: MainActivity:
package com.wcs.choosepictest;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Build;import android.os.Environment;import android.provider.MediaStore;import android.support.v4.content.FileProvider;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity { public static final int TAKE_PHOTO = 1;//照相 private Button takePhoto; private ImageView picture; private Uri imageUri;//图片的保存地址 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button)findViewById(R.id.take_photo); picture = (ImageView)findViewById(R.id.picture); takePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("MainActivity","Starting..."); //创建File对象,用于储存招片 File outputImage = new File(getExternalCacheDir(),"output_image.jpg"); try{ if(outputImage.exists()) outputImage.delete(); outputImage.createNewFile();//如果创建文件失败,则抛出异常 }catch (IOException e){ e.printStackTrace(); } if(Build.VERSION.SDK_INT >= 24){ imageUri = FileProvider.getUriForFile(MainActivity.this,"com.wcs.choosepictest.fileprovider",outputImage); }else { imageUri = Uri.fromFile(outputImage);//将图片地址用URI保存 } Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,TAKE_PHOTO);//启动相机 } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode,resultCode,data); switch(requestCode){ case TAKE_PHOTO: Log.d("MainActivity","resultCode is:"+resultCode); if(resultCode == RESULT_OK){ try{ Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); picture.setImageBitmap(bitmap); }catch (FileNotFoundException e){ e.printStackTrace(); } } break; default: break; } }}AndroidManifest:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wcs.choosepictest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/APPTheme"> <provider android:authorities="com.wcs.choosepictest.fileprovider" android:name="android.support.v4.content.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>file_paths:
<?xml version="1.0" encoding="utf-8"?><path xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path=""/></path>运行效果如下:
新闻热点
疑难解答