简体   繁体   English

从Intent获取APK的URI路径

[英]Get a URI path to APK from intent

How can I get path to APK file in Intent when I attached my activity to "Open with.." list? 将活动附加到“打开方式..”列表时,如何在Intent中获取APK文件的路径?

Code of manifest: 清单代码:

<activity
        android:name=".InstallActivity"
        android:label="@string/title_activity_install"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter >
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="application/vnd.android.package-archive" />
        </intent-filter>
</activity>

Code of activity (InstallActivity.java) 活动代码(InstallActivity.java)

public class InstallActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_install);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    }
}

Thanks! 谢谢!

You can get the Uri to the APK via getIntent().getData() . 您可以通过getIntent().getData()Uri转到APK。

If that Uri happens to have a file scheme, you can call getPath() on the Uri to get the path. 如果该Uri恰好具有file方案,则可以在Uri上调用getPath()以获取路径。

More likely, the Uri will have a content scheme, in which case you cannot get the path, as there does not have to be a file, let alone one that you can access via the filesystem. Uri更有可能采用一种content方案,在这种情况下,您将无法获得路径,因为不必存在文件,更不用说可以通过文件系统访问的文件了。 You will need to consume the content identified by the Uri using other APIs, such as ContentResolver and openInputStream() . 您将需要使用其他API(例如ContentResolveropenInputStream()使用Uri标识的内容

It is conceivable, though unlikely, that the Uri will have another scheme, such as android.resource or http . 尽管不太可能,但可以想象Uri将具有其他方案,例如android.resourcehttp android.resource will be handled by openInputStream() . android.resource将由openInputStream()处理。 http would require an HTTP client API, as would https . httphttps都需要HTTP客户端API。 And so on for any other schemes. 对于其他任何方案,依此类推。

Ideally, you augment your <intent-filter> to add <data> elements to constrain your app to the schemes that you support. 理想情况下,您可以扩展<intent-filter>以添加<data>元素,以将您的应用约束为您支持的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM