【Android】请求分享文件

概述

一个应用程序经常需要向其他应用程序提供一个甚至多个文件。例如,当我们用图片编辑器编辑图片时,被编辑的图片往往由图库应用程序所提供;再比如,文件管理器会允许用户在外部存储的不同区域之间复制粘贴文件。这里,我们提出一种让应用程序可以分享文件的方法:即令发送文件的应用程序对索取文件的应用程序所发出的文件请求进行响应。

在所有情况下,将文件从你的应用程序发送至其它应用程序的唯一的安全方法是向接收文件的应用程序发送这个文件的content URI,并对该URI授予临时的访问权限。具有URI临时访问权限的content URI是安全的,,因为他们仅应用于接收这个URI的应用程序,并且它们会自动过期。Android的FileProvider组件提供了)方法来创建一个文件的content URI。

服务APP

Manifest配置

<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="com.example.testpathcode.fileprovider"android:exported="false"android:grantUriPermissions="true" ><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/filepaths" /></provider>

在res/xml建filepaths.xml

<?xml version="1.0" encoding="utf-8"?><paths><files-path path="images/" name="myimages" /></paths>以上表示分享的文件是/data/data/pack_name/files/images/*

如果你从图片浏览器中获取过图片,你就知道我们还需要一个Activity来供我们选择文件

public class FileSelectActivity extends ListActivity implements OnItemClickListener {private File mPrivateRootDir;// The path to the "images" subdirectoryprivate File mImagesDir;// Array of files in the images subdirectoryFile[] mImageFiles;// Array of filenames corresponding to mImageFilesString[] mImageFilenames;// Initialize the ActivityIntent mResultIntent;ListView lv ;ArrayAdapter<String> adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);lv = getListView();lv.setOnItemClickListener(this);// Set up an Intent to send back to apps that request a filemResultIntent =new Intent("com.example.myapp.ACTION_RETURN_FILE");// Get the files/ subdirectory of internal storagemPrivateRootDir = getFilesDir();// Get the files/images subdirectory;mImagesDir = new File(mPrivateRootDir, "images");// Get the files in the images subdirectorymImageFiles = mImagesDir.listFiles();mImageFilenames = new String[mImageFiles.length];for (int i = 0; i < mImageFiles.length; i++) {mImageFilenames[i] = mImageFiles[i].getName();}// Set the Activity's result to null to begin withsetResult(Activity.RESULT_CANCELED, null); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mImageFilenames);lv.setAdapter(adapter);}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {File requestFile = mImageFiles[position];if (requestFile==null) {System.out.println("requestFile==null");return;}// <a target=_blank class="header" href="eclipse-javadoc:%E2%98%82=testpathcode/F:%5C/programs%5C/appcompat_v7%5C/libs%5C/android-support-v4.jar%3Candroid.support.v4.content(FileProvider.class%E2%98%83FileProvider">FileProvider</a>.getUriForFile(<span style="FONT-WEIGHT: normal"></span><a target=_blank title="in android.content" class="header" href="eclipse-javadoc:%E2%98%82=testpathcode/F:%5C/programs%5C/appcompat_v7%5C/libs%5C/android-support-v4.jar%3Candroid.support.v4.content(FileProvider.class%E2%98%83FileProvider~getUriForFile~Landroid.content.Context;~Ljava.lang.String;~Ljava.io.File;%E2%98%82android.content.Context">Context</a> context, <span style="FONT-WEIGHT: normal"></span><a target=_blank title="in java.lang" class="header" href="eclipse-javadoc:%E2%98%82=testpathcode/F:%5C/programs%5C/appcompat_v7%5C/libs%5C/android-support-v4.jar%3Candroid.support.v4.content(FileProvider.class%E2%98%83FileProvider~getUriForFile~Landroid.content.Context;~Ljava.lang.String;~Ljava.io.File;%E2%98%82java.lang.String">String</a> authority, <span style="FONT-WEIGHT: normal"></span><a target=_blank title="in java.io" class="header" href="eclipse-javadoc:%E2%98%82=testpathcode/F:%5C/programs%5C/appcompat_v7%5C/libs%5C/android-support-v4.jar%3Candroid.support.v4.content(FileProvider.class%E2%98%83FileProvider~getUriForFile~Landroid.content.Context;~Ljava.lang.String;~Ljava.io.File;%E2%98%82java.io.File">File</a> file)Uri fileUri = FileProvider.getUriForFile(this,"com.example.testpathcode.fileprovider",requestFile);//Log.v("URI", fileUri.toString());if (fileUri != null) {mResultIntent.setDataAndType(fileUri,getContentResolver().getType(fileUri));// Grant temporary read permission to the content URImResultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);setResult(Activity.RESULT_OK,mResultIntent);finish();}}真正的寂寞是在人群中,当你面对许多熟悉的脸,

【Android】请求分享文件

相关文章:

你感兴趣的文章:

标签云: