How To Create Vcf File In Android Programmatically
Hello, learners. Welcome togenerate vcf file android studio programmatically example tutorial.
We will learn how you cangenerate vcf file android studio programmatically from string resources.
VCF file also known as Vcard file.
Creating Vcard file in android app is an easy task.
This tutorial is helpful to you when you want to develop an app regarding contact details.
For example, when you want to share contact details which are given by user, this tutorial will guide you to bound those details in vCard file.
First check output then we will implement tutorial in the android studio.
Download Source Code
[sociallocker]Download VcfDemonuts [/sociallocker]
Creating generate vcf file android studio programmatically
Step 1: Create a new project in Android Studio.
Always create a new project in Android Studio with empty activity.
Step 2: Updating AndroidManifest.xml file
add required permissions between <manifest>….</manifest> tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
We need these two permissions to read and write details from external storage of user's mobile device.
Note: If you are targeting SDK version above 22 (Above Lollipop) then you need to ask a user for granting runtime permissions.
Final code for AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.exampledemo.parsaniahardik.vcfdemonuts"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 3: Updating activity_main.xml file
Copy below code in activity_main.xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.exampledemo.parsaniahardik.vcfdemonuts.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etname" android:hint="Name " /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etphon" android:hint="Phone number " /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etmail" android:hint="Email" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="Create vcf"/> </LinearLayout>
Step 4: Preparing MainActivity.java class
Add following source code in MainActivity.java class
import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private EditText etname, etphon,etmail; private Button btn; private static final String VCF_DIRECTORY = "/vcf_demonuts"; private File vcfFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etname = (EditText) findViewById(R.id.etname); etphon = (EditText) findViewById(R.id.etphon); etmail = (EditText) findViewById(R.id.etmail); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { // File vcfFile = new File(this.getExternalFilesDir(null), "generated.vcf"); File vdfdirectory = new File( Environment.getExternalStorageDirectory() + VCF_DIRECTORY); // have the object build the directory structure, if needed. if (!vdfdirectory.exists()) { vdfdirectory.mkdirs(); } vcfFile = new File(vdfdirectory, "android_"+ Calendar.getInstance().getTimeInMillis() + ".vcf"); FileWriter fw = null; fw = new FileWriter(vcfFile); fw.write("BEGIN:VCARD\r\n"); fw.write("VERSION:3.0\r\n"); // fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "\r\n"); fw.write("FN:" + etname.getText().toString() + "\r\n"); // fw.write("ORG:" + p.getCompanyName() + "\r\n"); // fw.write("TITLE:" + p.getTitle() + "\r\n"); fw.write("TEL;TYPE=WORK,VOICE:" + etphon.getText().toString() + "\r\n"); // fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "\r\n"); // fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "\r\n"); fw.write("EMAIL;TYPE=PREF,INTERNET:" + etmail.getText().toString() + "\r\n"); fw.write("END:VCARD\r\n"); fw.close(); /* Intent i = new Intent(); //this will import vcf in contact list i.setAction(android.content.Intent.ACTION_VIEW); i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard"); startActivity(i);*/ Toast.makeText(MainActivity.this, "Created!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } }); } }
Step 5: Description of MainActivity.java
Following code will genereate a folder named "vcf_demonuts" if not existed.
File vdfdirectory = new File( Environment.getExternalStorageDirectory() + VCF_DIRECTORY); // have the object build the directory structure, if needed. if (!vdfdirectory.exists()) { vdfdirectory.mkdirs(); }
Following line specifies name of the VCFor vCard file
vcfFile = new File(vdfdirectory, "android_"+ Calendar.getInstance().getTimeInMillis() + ".vcf");
Here, we will fetch current time as a name of vCard file. It also includes millisecond so that always, unique name will be generated.
Finally, below code will generate VCF or vCard file
FileWriter fw = null; fw = new FileWriter(vcfFile); fw.write("BEGIN:VCARD\r\n"); fw.write("VERSION:3.0\r\n"); // fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "\r\n"); fw.write("FN:" + etname.getText().toString() + "\r\n"); // fw.write("ORG:" + p.getCompanyName() + "\r\n"); // fw.write("TITLE:" + p.getTitle() + "\r\n"); fw.write("TEL;TYPE=WORK,VOICE:" + etphon.getText().toString() + "\r\n"); // fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "\r\n"); // fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "\r\n"); fw.write("EMAIL;TYPE=PREF,INTERNET:" + etmail.getText().toString() + "\r\n"); fw.write("END:VCARD\r\n"); fw.close();
If you want to open this newly generated vcf or vcard file programmatically, then remove the comment from below snippet.
/* Intent i = new Intent(); //this will import vcf in contact list i.setAction(android.content.Intent.ACTION_VIEW); i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard"); startActivity(i);*/
So all forgenerate vcf file android studio programmatically example tutorial.
Thank you and keep learning.
How To Create Vcf File In Android Programmatically
Source: https://demonuts.com/generate-vcf-file/
Posted by: pullenmrseach.blogspot.com
0 Response to "How To Create Vcf File In Android Programmatically"
Post a Comment