Java 如何通过打开图像启动myapp?
Java How to launch myapp by opening an image?
我正在开发一个图像编辑器应用程序,当用户从文件管理器或...中单击图像时,我希望我的应用程序出现在 'Open With ...' 列表中(例如 'Photo Editor' 应用程序做到了):
Click to see the example (Edited)
问题是我不知道该怎么做。
我在youtube上查了一下,发现是关于Manifest.xml
文件中的intent filter
。
我已经尝试随机添加一些 <action/>
到我的 intent 过滤器,但没有成功。
转到 Manifest
文件并搜索您的 LAUNCHER
activity 例如 SplashActivity
像这样
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
//this below code help you to get image from other apps if shared
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*" />
</intent-filter>
</activity>
要在 SplashActivity 中获取该数据,请执行以下操作:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
//this will check that you are getting only image type not text
boolean isImage = Intent.ACTION_SEND.equals(action) && "image/*".equals(type);
if(getIntent().getData() != null && isImage){
// here you got the data and you can send this data or uri
// to your image editor activity
Uri imageUri = intent.getData();
}
然后您可以通过这样做
将该图像发送到下一个activity
您可以将 URI 存储为字符串
intent.putExtra("imageUri", imageUri.toString());
然后在您的 (EditorActivity)
中像这样将字符串转换回 URI
Uri myUri = Uri.parse(getIntent().getStringExtra("imageUri"));
我正在开发一个图像编辑器应用程序,当用户从文件管理器或...中单击图像时,我希望我的应用程序出现在 'Open With ...' 列表中(例如 'Photo Editor' 应用程序做到了):
Click to see the example (Edited)
问题是我不知道该怎么做。
我在youtube上查了一下,发现是关于Manifest.xml
文件中的intent filter
。
我已经尝试随机添加一些 <action/>
到我的 intent 过滤器,但没有成功。
转到 Manifest
文件并搜索您的 LAUNCHER
activity 例如 SplashActivity
像这样
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
//this below code help you to get image from other apps if shared
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*" />
</intent-filter>
</activity>
要在 SplashActivity 中获取该数据,请执行以下操作:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
//this will check that you are getting only image type not text
boolean isImage = Intent.ACTION_SEND.equals(action) && "image/*".equals(type);
if(getIntent().getData() != null && isImage){
// here you got the data and you can send this data or uri
// to your image editor activity
Uri imageUri = intent.getData();
}
然后您可以通过这样做
将该图像发送到下一个activity您可以将 URI 存储为字符串
intent.putExtra("imageUri", imageUri.toString());
然后在您的 (EditorActivity)
中像这样将字符串转换回 URIUri myUri = Uri.parse(getIntent().getStringExtra("imageUri"));