第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

android app下載apk文件并打開(kāi)(安裝)

android app下載apk文件并打開(kāi)(安裝)

慕慕森 2022-06-30 18:04:15
我想知道您是否可以提供幫助,我正在嘗試在版本檢查后下載一個(gè) apk 文件作為更新。它正在下載 apk,但它不會(huì)打開(kāi) apk 文件它在嘗試打開(kāi) apk 時(shí)打印錯(cuò)誤錯(cuò)誤 :E/UpdateAPP: Update error! file:///storage/emulated/0/download/update.apk exposed beyond app through Intent.getData()代碼 :public class UpdateApp extends AsyncTask<String,Void,Void> {    private Context context;    public void setContext(Context contextf){        context = contextf;    }    @Override    protected Void doInBackground(String... arg0) {        try {            URL url = new URL(arg0[0]);            HttpURLConnection c = (HttpURLConnection) url.openConnection();            c.setRequestMethod("GET");            c.setDoOutput(true);            c.connect();            String PATH = Environment.getExternalStorageDirectory() + "/download/";            File file = new File(PATH);            file.mkdirs();            File outputFile = new File(file, "update.apk");            if(outputFile.exists()){                outputFile.delete();            }            FileOutputStream fos = new FileOutputStream(outputFile);            InputStream is = c.getInputStream();            byte[] buffer = new byte[1024];            int len1 = 0;            while ((len1 = is.read(buffer)) != -1) {                fos.write(buffer, 0, len1);            }            fos.close();            is.close();            Intent intent = new Intent(Intent.ACTION_VIEW);            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "/update.apk")), "application/vnd.android.package-archive");            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!            context.startActivity(intent);        } catch (Exception e) {            Log.e("UpdateAPP", "Update error! " + e.getMessage());        }        return null;    }}我的 targetSdkVersion :targetSdkVersion 27謝謝您的幫助
查看完整描述

1 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

從 API 26 開(kāi)始,“REQUEST_INSTALL_PACKAGES”權(quán)限是實(shí)現(xiàn)安裝 apk 文件的必要權(quán)限。


在 res/xml 文件夾中創(chuàng)建“file_paths.xml”以使用 FileProvider api

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path name="" path="/" />

</paths>

在 AndroidManifest.xml 中聲明 FileProvider 和權(quán)限

筆記。我使用的是 Androidx 版本的 FileProvider,如果你不使用 androidx,請(qǐng)確保 android.support.v4.content.FileProvider


<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />


 <provider

            android:name="androidx.core.content.FileProvider"

            android:authorities="{PACKAGE_NAME}.fileprovider"

            android:exported="false"

            android:grantUriPermissions="true">

            <meta-data

                android:name="android.support.FILE_PROVIDER_PATHS"

                android:resource="@xml/file_paths" />


</provider>

通過(guò) FileProvider api 獲取“Uri”并請(qǐng)求安裝權(quán)限

private fun openFile(file: File) {

        val uri = if (Build.VERSION.SDK_INT >= 24) {

            val authority = packageName + ".fileprovider"

            FileProvider.getUriForFile(this, authority, file)

        } else {

            Uri.fromFile(file)

        }


        val myMime = MimeTypeMap.getSingleton()

        val mimeType = myMime.getMimeTypeFromExtension(file.extension)


        val intent = Intent(Intent.ACTION_VIEW).apply {

            setDataAndType(uri, mimeType)

            flags = Intent.FLAG_ACTIVITY_NEW_TASK

            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

        }


        if (Build.VERSION.SDK_INT >= 26 && !packageManager.canRequestPackageInstalls()) {

            startActivity(

                Intent(

                    Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,

                    Uri.parse("package:$packageName")

                )

            )

        } else {

            intent.action = Intent.ACTION_VIEW

            startActivity(intent)

        }

    }

編輯 -> 刪除在我的項(xiàng)目中使用的特定方法。


查看完整回答
反對(duì) 回復(fù) 2022-06-30
  • 1 回答
  • 0 關(guān)注
  • 271 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)