【Android Developers Training】37.一つのファイルを共有する
21759 ワード
この文章はGoogleの公式Android Developers Trainingの文書から訳して、翻訳者の技術は普通で、Androidが好きなため翻訳の考えを生んで、全く個人の興味の趣味です.
リンク:http://developer.android.com/training/secure-file-sharing/share-file.html
URI共有ファイルを使用するアプリケーションを設定すると、他のアプリケーションの要求に応じてこれらのファイルを共有することができます.応答の方法は、サービスアプリケーションにファイル選択インターフェースを提供し、他のアプリケーションによって活性化され得る.この方法は、サービスアプリケーションからユーザにファイルを選択させてから、このファイルのURIを受信することを可能にする.
この授業では、あなたのアプリケーションでファイルを選択するためのActivityをどのように作成するかを示します.これらの要求に応じて、ファイルの要求に応えます.
一)ファイルの要求を受信する
クライアントアプリケーションからファイル要求要求要求要求を受信し、URI形式で応答するために、あなたのアプリケーションは選択ファイルのActivityを提供するべきである.クライアントアプリケーションは、startActivity ForResoult()
リンク:http://developer.android.com/training/secure-file-sharing/share-file.html
URI共有ファイルを使用するアプリケーションを設定すると、他のアプリケーションの要求に応じてこれらのファイルを共有することができます.応答の方法は、サービスアプリケーションにファイル選択インターフェースを提供し、他のアプリケーションによって活性化され得る.この方法は、サービスアプリケーションからユーザにファイルを選択させてから、このファイルのURIを受信することを可能にする.
この授業では、あなたのアプリケーションでファイルを選択するためのActivityをどのように作成するかを示します.これらの要求に応じて、ファイルの要求に応えます.
一)ファイルの要求を受信する
クライアントアプリケーションからファイル要求要求要求要求を受信し、URI形式で応答するために、あなたのアプリケーションは選択ファイルのActivityを提供するべきである.クライアントアプリケーションは、startActivity ForResoult()
Activity。 Intent
を び すことによって、ACTION_PICK を する. アプリケーションがstartActivity ForResoult() , , URI。
を び したとき
どのようにお のアプリケーションでファイルの を するかを します.
)ファイルを してActivityを する
ファイル Requesting a Shared Fileのために、リストファイル Activityからスタートします.そのintentフィルタにおいて、マッチングActivityの 、およびACTION_PICKとCATEGORY_DEFAULTのタイプです.また、あなたのアプリケーションのために のアプリケーションに されるファイルにMIMEタイプフィルタを します. のコードは、リストファイルに しいCATEGORY_OPEABLEとintentフィルタを する を している.<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<application>
...
<activity
android:name=".FileSelectActivity"
android:label="@"File Selector" >
<intent-filter>
<action
android:name="android.intent.action.PICK"/>
<category
android:name="android.intent.category.DEFAULT"/>
<category
android:name="android.intent.category.OPENABLE"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
コードで したファイル Activity
に、Activityサブクラスを して、あなたの に されている「files/imags/」ディレクトリで できるファイルを し、ユーザーが のファイルを できるようにします. のコードは、これをどのように するかを しているActivity.そしてユーザの に します.public class MainActivity extends Activity {
// The path to the root of this app's internal storage
private File mPrivateRootDir;
// The path to the "images" subdirectory
private File mImagesDir;
// Array of files in the images subdirectory
File[] mImageFiles;
// Array of filenames corresponding to mImageFiles
String[] mImageFilenames;
// Initialize the Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Set up an Intent to send back to apps that request a file
mResultIntent =
new Intent("com.example.myapp.ACTION_RETURN_FILE");
// Get the files/ subdirectory of internal storage
mPrivateRootDir = getFilesDir();
// Get the files/images subdirectory;
mImagesDir = new File(mPrivateRootDir, "images");
// Get the files in the images subdirectory
mImageFiles = mImagesDir.listFiles();
// Set the Activity's result to null to begin with
setResult(Activity.RESULT_CANCELED, null);
/*
* Display the file names in the ListView mFileListView.
* Back the ListView with the array mImageFilenames, which
* you can create by iterating through mImageFiles and
* calling File.getAbsolutePath() for each File
*/
...
}
...
}
)ファイルの に する
ユーザーが ファイルを すると、アプリケーションはどのファイルが されたかを にして、このファイルのために するURIを しなければなりません.ActivityがActivityにファイルを できるリストを した 、ユーザがファイル をクリックしたとき、システムは ListViewを び し、この で されたファイルを することができる.
onItemClick()において、 されたファイル のためにonItemClick()オブジェクトを し、それをパラメータとしてファイルに すとともに、 したいパラメータはgetUriForFile()に されたFileProviderタグ である.この URIは、 するアクセス と、ファイルディレクトリに するパスタグ(XML meta-dateで されているような)と、 を むファイル を んでいる.<プロバイダー>がどのようにXML meta-dataベースのディレクトリパスの を するかについては、FileProviderを むことができます.
の では、 したファイルをどのように し、URIを しますか? protected void onCreate(Bundle savedInstanceState) {
...
// Define a listener that responds to clicks on a file in the ListView
mFileListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
/*
* When a filename in the ListView is clicked, get its
* content URI and send it to the requesting app
*/
public void onItemClick(AdapterView<?> adapterView,
View view,
int position,
long rowId) {
/*
* Get a File for the selected file name.
* Assume that the file names are in the
* mImageFilename array.
*/
File requestFile = new File(mImageFilename[position]);
/*
* Most file-related method calls need to be in
* try-catch blocks.
*/
// Use the FileProvider to get a content URI
try {
fileUri = FileProvider.getUriForFile(
MainActivity.this,
"com.example.myapp.fileprovider",
requestFile);
} catch (IllegalArgumentException e) {
Log.e("File Selector",
"The selected file can't be shared: " +
clickedFilename);
}
...
}
});
...
}
えてください.あなたが できるURIに するファイルは、meta-dataファイルに「paths」タグを むディレクトリ のファイルです.この はSpecify Sharable Directores(ブログリンク:Specify Sharable Directores)で されています.もしあなたが されていないディレクトリ のファイルのためにhttp://www.cnblogs.com/jdneo/p/3480405.htmlメソッドを び したら、getUriForFile()を します.
)ファイルの
は のアプリケーションに したいファイルURIがあります.お のアプリケーションがこのファイルにアクセスできるようにする があります.アクセスを するために、URIをIllgalAgmentExceptionに し、 いでIntentにパーミッションフラグを することができる. えられた は で、アプリケーションを したタスクスタックが すると に が れます.
の は、ファイルの み み を する を しています. protected void onCreate(Bundle savedInstanceState) {
...
// Define a listener that responds to clicks in the ListView
mFileListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view,
int position,
long rowId) {
...
if (fileUri != null) {
// Grant temporary read permission to the content URI
mResultIntent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
...
}
...
});
...
}
Caution:
Intentを び すことは の な であり、あなたのファイルに なアクセス を える.ファイルURIに してset Flags()を び すことは けられます.この によって えられた は、Conttext.grantUriPermission()を び してキャンセルするしかないからです.
)デマンドアプリケーションと ファイル
なファイルをデマンドアプリケーションと するために、URIと を むConttext.revokeUriPermission()をIntentに す. されたset Result()が すると、システムは、URIを むActivityをクライアントアプリケーションに する. の はあなたがどうするべきかを しています.protected void onCreate(Bundle savedInstanceState) {
...
// Define a listener that responds to clicks on a file in the ListView
mFileListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view,
int position,
long rowId) {
...
if (fileUri != null) {
...
// Put the Uri and MIME type in the result Intent
mResultIntent.setDataAndType(
fileUri,
getContentResolver().getType(fileUri));
// Set the result
MainActivity.this.setResult(Activity.RESULT_OK,
mResultIntent);
} else {
mResultIntent.setDataAndType(null, "");
MainActivity.this.setResult(RESULT_CANCELED,
mResultIntent);
}
}
});
らがファイルを すると、すぐにクライアントに る をユーザーに します. の は、チェックボックスまたは ボタンを することである.ボタンを するIntent フィールドは、それに する である.この では、android:onClickが び される.たとえば:public void onDoneClick(View v) {
// Associate a method with the Done button
finish();
}