How do I open a .vcf from an intent filer? - android

Hello I'm trying to make an app where I can open .vcf files. Primarily from texting apps and I've gotten it to work where I can open my app using the following code in and androidmanifest.xml
<activity android:name=".Home"
android:parentActivityName=".MapsActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="MapsActivity" />
<intent-filter android:label="Open iLocation">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
<intent-filter android:label="Open iLocation">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="text/x-vcard" />
<data android:pathPattern=".*\.vcf" />
<data android:pathPattern=".*\..*\.vcf" />
<data android:pathPattern=".*\..*\..*\.vcf" />
<data android:pathPattern=".*\..*\..*\..*\.vcf" />
<data android:pathPattern=".*\..*\..*\..*\..*\.vcf" />
<data android:host="*" />
</intent-filter>
</activity>
That opens my .Home file but I'm not sure how I get the data from the file. Any help would greatly be appreciated.

To get the Uri representing the content you are to view or edit, call getIntent().getData(). Then, you can use ContentResolver and openInputStream() to get an InputStream on the actual content you are offering to view or edit, given that Uri.
In terms of vCard itself, Android has nothing built-in for vCard parsing, in terms of a traditional Java API. Your choices are:
Use this third-party library
Find another third-party library or other vCard parsing hunk of code (e.g., the Contacts app should have some in there somewhere)
Roll your own parser sufficient for your needs

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
mIntent.setAction(Intent.ACTION_GET_CONTENT);
mIntent.setType("text/x-vcard");
startActivityForResult(mIntent, 1);
} else {
mIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
mIntent.addCategory(Intent.CATEGORY_OPENABLE);
mIntent.setDataAndType(Uri.fromFile(contactFolder), "text/x-vcard");
startActivityForResult(Intent.createChooser(mIntent, "Select File"), 2);
}
And get intent result.....
#SuppressLint("NewApi")
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri mUri = data.getData();
if (mUri != null) {
String mFilePath;
if (requestCode == 1) {
mFilePath = mUri.getPath();
} else {
mFilePath = getPathFromURI(mUri);
}
Intent mIntent = new Intent(this, RestoreActivity.class);
mIntent.putExtra("filepath", mFilePath);
startActivity(mIntent);
} else {
Toast.makeText(this, "Something went wrong...\nPlease try again...", Toast.LENGTH_SHORT).show();
}
}
}
#SuppressLint("NewApi")
private String getPathFromURI(Uri mUri) {
String mDocId = DocumentsContract.getDocumentId(mUri);
String[] mSplit = mDocId.split(":");
return Environment.getExternalStorageDirectory() + File.separator + mSplit[1];
}

It's actually not a very complicated process. Just ensure that you get the Uri from FileProvider and you'll be able to trigger the Phone Book Chooser Intent.
Then, you'll be able to import your contacts with .vcf.
private fun saveVcfFile(savedVCard: File) {
try {
val intent = Intent(Intent.ACTION_VIEW)
val uri = FileProvider.getUriForFile(
this,
"${BuildConfig.APPLICATION_ID}.fileprovider",
savedVCard
)
intent.setDataAndType(uri, contentResolver.getType(uri))
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
} catch (exception: Exception) {
// TODO: Handle Exception
}
}

Related

Start an app within another app Android

My question is how to start app A within app B that appears app A is within app B through deep linking?
In the first picture below, the Debug app appears as a separate app from Slack (new code, Firebase deep linking). In the 2nd picture, the Debug app appears to be within the Slack app (old code, Android deep linking). I want to use Firebase deep linking and show the Debug app within other apps (Slack, Gmail etc).
Can anyone please go through my code below and let me know how I can achieve this?
Old code, Android deep linking:
AndroidManifest
<activity
android:name=".activity.SplashScreenActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "abc://flights” -->
<data
android:host="sales"
android:scheme="abc" />
<data
android:host="deals"
android:scheme="abc" />
</intent-filter>
</activity>
Activity:
Intent intent = new Intent(SplashScreenActivity.this, BottomNavBarActivity.class);
//Deep Linking Content
Uri deepLinkData = getIntent().getData();
if (deepLinkData != null) {
intent.putExtra(EXTRA_DEEP_LINK, deepLinkData.getHost());
}
startActivity(intent);
overridePendingTransition(R.anim.splash_fade_in, R.anim.splash_fade_out);
finish();
New Code, Firebase deep linking:
AndroidManifest:
<activity
android:name=".activity.SplashScreenActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="abc.app.goo.gl"
android:scheme="http"/>
<data
android:host="abc.app.goo.gl"
android:scheme="https"/>
</intent-filter>
</activity>
Activity:
FirebaseDynamicLinks.getInstance()
.getDynamicLink(getIntent())
.addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
#Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
// Get deep link from result (may be null if no link is found)
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
// Start the activity through intent, same as before.
}
}
})
.addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.v(TAG, "Firebase deep link failure");
}
});
Its dependent on the app which sends the intent, for example whether they pass FLAG_ACTIVITY_NEW_TASK or not. I suspect the difference here is how Slack is handling the links - they may treat web URLs differently than other format ones (your old links have non-standard schemes).
Create a method openApp(), and call it accorrding to your need.
public void openAnApp()
{
Boolean flag=false;
try{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
final PackageManager packageManager = getActivity().getPackageManager();
Intent intent1 = new Intent(Intent.ACTION_MAIN, null);
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent1, 0);
ActivityInfo activity = null;
//getting package names and adding them to the hashset
for(ResolveInfo resolveInfo : resInfos) {
System.out.println("apap="+resolveInfo.activityInfo.packageName);
if(resolveInfo.activityInfo.packageName.equals("your.app.packagename"))
{
flag = true;
activity = resolveInfo.activityInfo;
break;
}
}
if (flag) {
// final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName,activity.name);
intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(name);
getActivity().startActivity(intent);
//startActivity(Intent.createChooser(intent , "Send image using.."));
} else {
Uri uri=Uri.parse("market://details?id=your.app.packagename");
Intent goToMarket=new Intent(Intent.ACTION_VIEW,uri);
try{
startActivity(goToMarket);
}catch(ActivityNotFoundException e){
Toast.makeText(getActivity(),"Couldn't launch the market",Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

Android deep linking is not working

Android deep linking is not working
===================================
Android link:-notification://id=notificationid
1:-In manifest
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="id"
android:scheme="notification" />
</intent-filter>
2:-and coding side
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String string=intent.getAction();
Uri data = intent.getData();
Log.d("hello","cgbdsuyfdkv");
}
but its not working please any body can help me !!!!!
you can simply modify your url
Android link:-notification://id=notificationid instead of
Android link:-notification://page?id=notificationid //page is host name
I tried a lot and find out this is working for me.
manifest code
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="page"
android:scheme="notification" />
</intent-filter>
2.java code #get your notification id here
Intent intent = getIntent();
String string = intent.getAction();
Uri data = intent.getData();
if (data != null) {
String path = data.getPath();
String path1 = data.getPath();
providerUrl = intent.getData().toString();
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
sanitizer.setAllowUnregisteredParamaters(true);
sanitizer.parseUrl(providerUrl);
String id = sanitizer.getValue("id");
if (id != null)
Log.d("notification id", id);
}
Add onNewIntent() method to your Activity and then use intent object which is variable in OnNewintent() method , which has updated method.
onNewIntent() is meant as entry point for singleTop or singleTask activities which already run somewhere else in the stack and therefore can't call onCreate(). From activities lifecycle point of view it's therefore needed to call onPause() before onNewIntent(). I suggest you to rewrite your activity to not use these listeners inside of onNewIntent(). For example most of the time my onNewIntent() methods simply looks like this:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//use this intent object to get notificationid for second time
}
1st step:
<intent-filter>
<data android:scheme="your_uri_scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
2nd step:
Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
String uri = this.getIntent().getDataString();
Log.i("MyApp", "Deep link clicked " + uri);
}
I think you Intent Filter is incomplete
<!-- To open app using link -->
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="http"
android:host="yourdomain.com"
android:pathPrefix="/someurlparam">
</data>
</intent-filter>

Block mobile website to open my app android deeplink - Google Chrome

I have supported deeplinks in my app
<activity android:name=".DeepLinkActivity" android:noHistory="true"></activity>
<activity-alias
android:name="com.example.Launcher"
android:targetActivity=".DeepLinkActivity">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
<data android:scheme="http" />
<data android:scheme="#string/SCHEMA" />
<data android:host="#string/WEB_DOMAIN" />
<data android:host="#string/WWW_WEB_DOMAIN" />
<data android:path="/" />
<data android:path="/x" android:pathPattern="/x/.*" />
</intent-filter>
</activity-alias>
So whenever user clicks on www.example.com the android app asks to open as app or web that is fine, but I do not want when my users are on mobile site they should be asked to open in app. I have gone through many stackoverflow posts but everyone says its not possible but still many websites are handling this scenario.
As per this Article the behaviour depends on user gestures, if user is clicking on any link then Chrome displays a chooser while if user is typing url on browser then it doesn't.
After lot of research I have solved it. You can use either way.
Handle in mobile website : So if you want to render your user to Chrome always when they are on your msite you can achieve this by redirecting all your urls to
googlechrome://navigate?url=https://www.example.com
intent://www.example.com/x#Intent;scheme=https;package=com.android.chrome;S.browser_fallback_url=https://www/example.com/x;action=android.intent.action.VIEW;end;
Handle in app
Create single transparent activity to handle all your deeplinks
Handle all the links using pathpattern = '.*'
Redirect user back to Chrome for urls you do not want to handle in app.
AndroidManifest.xml
<activity
android:name=".DeepLinkActivity"
android:noHistory="true"
android:theme="#style/Theme.Transparent">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
<data android:scheme="http" />
<data android:scheme="#string/SCHEMA" />
<data android:host="#string/WEB_DOMAIN" />
<data android:host="#string/WWW_WEB_DOMAIN" />
<data android:pathPattern="/.*" />
</intent-filter>
</activity>
DeepLinkActivity
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
try {
if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
Uri uri = getIntent().getData();
if (uri == null) throw new IllegalArgumentException("Uri can not be null");
Intent intent = null;
if (getString(R.string.SCHEMA).equals(uri.getScheme()) || uri.toString().matches(Links.REGEX)) {
intent = linkIntent(uri);
}
if (intent == null) SystemUtil.launchUrlInDefaultBrowser(uri, this); else startActivity(intent);
}
} catch (IllegalArgumentException e) {
Toast.makeText(this, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
} finally {
finish();
}
}
/**
* This will open the provided url in browser except the current app.
* #param url Uri
* #param context Activity Context
*/
public static void launchUrlInDefaultBrowser(Uri url, Context context) {
try {
ResolveInfo packageInfo = null;
final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(url);
// Try to find anything that we can launch the URL with. Pick up the first one that can.
final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (!resolveInfoList.isEmpty()) {
for (ResolveInfo list : resolveInfoList) {
if (!BuildConfig.APP_PACKAGE_NAME.equals(list.activityInfo.packageName)) {
packageInfo = list;
break;
}
}
}
if (packageInfo != null) {
browserIntent.setClassName(packageInfo.activityInfo.packageName, packageInfo.activityInfo.name);
context.startActivity(browserIntent);
} else {
Toast.makeText(context, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(context, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
}
}

How to use intent for sharing gallery picture to application

I have added my application to the share icon on top in the gallery, but so far it only opens my application, but how do I send the picture URI and stuff to my application? I understand I have to use intent? Are there any guides around I can look at? The easy-share-action guide on google website isn't helping me much here.
Try this way:
<activity
android:name="yourActivity"
android:icon="#drawable/ic_launcher"
android:label="test">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
Handle the Incoming Content
void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
...
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
}
}
For more information go to Receiving Simple Data from Other Apps and Sending Simple Data to Other Apps
To handle an image in your Activity you need to check if the intent has the image.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String action = intent.getAction();
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
// Get resource path of the stream, which in this case will be the image
}
}
Furthermore you need to add to your manifest that you want to filter on these sharing image intents.
<activity android:name=".Theme"
android:label="YourImageActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>

Register to be default app for custom file type

Register to be able to open files of custom type. Say i have .cool files, and if the user tries to open it, Android asks if they would like to open it with my application. How?
You can add the following to the AndroidManifest.xml file inside the activity that has to open the file (pdf in our case)
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />
</intent-filter>
Make sure you specify the proper mime format. The user will be prompted to choose your app if she wants to open the "pdf" file.
Check also Android intent filter: associate app with file extension for more details.
I think that you don't choose that. This is handle by the system. Same thing when you send an intent to be handled by a map, If you have skyfire for instance, the system pops up a window an you can choose the application and choose if you want it to always open this kind of file.
Unless of course if your application is the only one to open this kind of file.
Edit
I think if you want your app to say "hey I'm able to open these .cool files", you need to set up <intent-filters> with a tag <data> and specificy the mimeType or Uri. Check here for more details.
Here is a fuller example to show how to register your app to open plain text files.
Register the type of file you can handle
Add an intent-filter to your activity in the manifest. In my case it is the ReaderActivity that will display the text file. The mimeType says that I will accept any plain text file. See this for specific file extensions.
<activity android:name=".ReaderActivity">
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
Handle the intent
Get the data from the intent in your activity like this:
public class ReaderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
handleIntent();
}
private void handleIntent() {
Uri uri = getIntent().getData();
if (uri == null) {
tellUserThatCouldntOpenFile();
return;
}
String text = null;
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
text = getStringFromInputStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (text == null) {
tellUserThatCouldntOpenFile();
return;
}
TextView textView = findViewById(R.id.tv_content);
textView.setText(text);
}
private void tellUserThatCouldntOpenFile() {
Toast.makeText(this, getString(R.string.could_not_open_file), Toast.LENGTH_SHORT).show();
}
public static String getStringFromInputStream(InputStream stream) throws IOException {
int n = 0;
char[] buffer = new char[1024 * 4];
InputStreamReader reader = new InputStreamReader(stream, "UTF8");
StringWriter writer = new StringWriter();
while (-1 != (n = reader.read(buffer))) writer.write(buffer, 0, n);
return writer.toString();
}
}
You get the data from the intent with getData() to get the URI to the file. Then you use the content resolver to open an input stream. You use the content resolver because you might not have direct access to the file otherwise. Thanks to this answer for the method to convert the input stream to a string.
One possible answer is shown
here
. Use this inside one of your activity tag and it'll be opened when the file have been pressed. I tested it using the native android file manager in Oreo.
<intent-filter android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data android:host="*" />
<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\.yourextension" />
<data android:pathPattern=".*\\.yourextension" />
<data android:scheme="content" />
</intent-filter>

Categories

Resources