Android
[Android] androidx 에서 File 공유하기, File Share
bryan.oh
2020. 3. 3. 11:19
반응형
androidx file share
androidx 파일 공유
결과 화면입니다.
app > res > xml > file_provider.xml 을 생성합니다.
file_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="Android/data/${applicationId}/" name="files_root" />
<root-path name="root" path="/" />
</paths>
AndroidManifest.xml
app > manifests > AndroidManifest.xml 에 <provider> 부분을 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ai.bryan.elascan">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
// .. 생략
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
<!-- 이부분을 추가합니다 -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider" />
</provider>
<!-- 여기까지 추가합니다 -->
</application>
</manifest>
사용( Activity 에서 )
File extRoot = getExternalFilesDir(null);
String someFile = "/test/some.xls";
File xlsFile = new File(extRoot, someFile);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/excel"); // 엑셀파일 공유 시
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".fileprovider", xlsFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent,"엑셀 공유"));
setType
file type | setType |
이미지 | image/* |
동영상 | video/* |
오디오 | audio/* |
파일 | application/* |
* 대신에 직접 명시해도 됩니다.
예) 엑셀이면 application/excelsetType("application/excel")
이미지는 여러장 공유가 됩니다.
Intent 를 Intent.ACTION_SEND_MULTIPLE 로 하고 putExtra를 여러번 호출하면 됩니다.
ArrayList<File> fileList = //... 정의하시고
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("image/*"); // 이미지만 여러장 공유 가능
for(File f : fileList){
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".fileprovider", f);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
}
startActivity(Intent.createChooser(shareIntent,"이미지 공유"));
startActivity 대시 startActivityForResult 를 사용해서 결과를 받아올 수 있습니다.
728x90
반응형