[英]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(例如
ContentResolver
和openInputStream()
使用Uri
标识的内容 。
It is conceivable, though unlikely, that the Uri
will have another scheme, such as android.resource
or http
. 尽管不太可能,但可以想象
Uri
将具有其他方案,例如android.resource
或http
。 android.resource
will be handled by openInputStream()
. android.resource
将由openInputStream()
处理。 http
would require an HTTP client API, as would https
. http
和https
都需要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.