前一阵子接触这个东西,开始一直不理解是什么意思,一般来说我们运用Uri.fromFile来直接将文件路径加载为Uri,但是安卓6.0之后,就不能这样做了,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。
而进行此授权的最简单方式是使用 FileProvider类。(修改后的伪代码在讲述FileProvider的使用时会写)
所以应用件的共享数据就必须用到这个类,直接上例子进行验证
我们以打开摄像机机型拍照为例
package com.example.fang.myapplication;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
File file;
Uri uri;
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView);
button= (Button) findViewById(R.id.button);
button.setOnTouchListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0: {
if(resultCode==RESULT_OK){
try {
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
file = new File(getBaseContext().getExternalCacheDir(), "image.jpg");//使用的是应用缓存目录
uri = Uri.fromFile(file);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, 0);
return true;
}
}
运行结果如下
tmd出错了,刚才还能拍照,以为写错了,看来是对的了,另外我们悬着了应用的缓存目录,因为6.0开始,读写SD卡属于危险权限,需要运行时权限,而通过这一点,我们可以跳过这一步
现在我们使用应用fileProvider来实现这种功能,
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fang.myapplication.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
java文件
package com.example.fang.myapplication;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
File file;
Uri uri;
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView);
button= (Button) findViewById(R.id.button);
button.setOnTouchListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0: {
if(resultCode==RESULT_OK){
try {
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
file = new File(getBaseContext().getExternalCacheDir(), "image.jpg");//使用的是应用缓存目录
// uri = Uri.fromFile(file);
uri=FileProvider.getUriForFile(this,"com.example.fang.myapplication",file);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 0);
return true;
}
}
由于这里是FileProvider,所以同样,我们需要在配置文件中进行注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fang.myapplication">
<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>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.fang.myapplication"
android:exported="false" android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>
在Provider中有个meta-data,里面的就是共享地址,我们在里面引用了@xml/file_paths文件,创建代码如下
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="my_images" path=""/>
</paths>
在这里external-path是指定uri共享的,有三种共享方式
file-path代表的根目录:context.getFilesDir();分享app内部存储
external-path代表的根目录:environment.getExternalStorageDirectory();分享外部存储
cache-path代表的根目录:getcacheDir()//分享内部缓冲目录
//注意,这里对应的文件目录要与共享的文件目录所对应,不然会报错