This post contains affiliate marketing, and I may receive a commission at no additional cost to you. 이 포스팅은 제휴마케팅이 포함되어 일정의 커미션을 지급받습니다.
안드로이드 7.0 누가 이상버전에서 패키지 설치화면 띄우기
res/xml폴더에 filepaths.xml을 만들고 아래코드 입력 <?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="storage/emulated" path="."/> </paths> |
AndroidManifest.xml <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> |
apk파일 install하는 코드 private void install(String apkName){ String localUrl = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + apkName; String extension = MimeTypeMap.getFileExtensionFromUrl(localUrl); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); File file = new File(localUrl); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ Uri apkUri = FileProvider.getUriForFile(app.getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file); Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(apkUri); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); try { startActivity(intent); } catch (ActivityNotFoundException e) { toastS(getResources().getString(R.string.app_name)+" 설치파일을 찾을수 없습니다"); e.printStackTrace(); } }else{ Uri apkUri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(apkUri, mimeType); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(intent); } catch (ActivityNotFoundException e) { toastS(getResources().getString(R.string.app_name)+" 설치파일을 찾을수 없습니다"); e.printStackTrace(); } } } |
'개발이야기 > Android' 카테고리의 다른 글
외부저장소 sd(메모리)카드에 있는 파일 읽어들일때 FileProvider 오류 처리 (0) | 2019.04.05 |
---|---|
android - cannot resolve symbol FileProvider (0) | 2019.03.26 |
안드로이드 9.0 HTTP 통신 대응 (0) | 2019.03.22 |