简体   繁体   中英

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?

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)

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() .

If that Uri happens to have a file scheme, you can call getPath() on the Uri to get the path.

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. You will need to consume the content identified by the Uri using other APIs, such as ContentResolver and openInputStream() .

It is conceivable, though unlikely, that the Uri will have another scheme, such as android.resource or http . android.resource will be handled by openInputStream() . http would require an HTTP client API, as would https . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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