Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kapil.demo.demo;
- import android.Manifest;
- import android.app.Activity;
- import android.content.pm.PackageManager;
- import android.content.res.AssetFileDescriptor;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.ContactsContract;
- import android.support.v4.app.ActivityCompat;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.widget.Toast;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import static android.content.ContentValues.TAG;
- public class MainActivity extends Activity {
- Cursor cursor;
- ArrayList<String> vCard ;
- String vfile;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- protected void onStart() {
- super.onStart();
- shoro();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- public boolean isStoragePermissionGranted() {
- if (Build.VERSION.SDK_INT >= 23) {
- Log.v("kapil","Permission is granted 1 "+checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE));
- Log.v("kapil","Permission is granted 2 "+checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS));
- Log.v("kapil","Permission is granted 3 "+checkSelfPermission(android.Manifest.permission.READ_CONTACTS));
- if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
- == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.WRITE_CONTACTS)
- == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_CONTACTS)
- == PackageManager.PERMISSION_GRANTED) {
- Log.v(TAG,"Permission is granted");
- return true;
- } else {
- Log.v(TAG,"Permission is revoked");
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS}, 1);
- return false;
- }
- }
- else { //permission is automatically granted on sdk<23 upon installation
- Log.v(TAG,"Permission is granted");
- return true;
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case 0:
- boolean isPerpermissionForAllGranted = false;
- if (grantResults.length > 0 && permissions.length==grantResults.length) {
- for (int i = 0; i < permissions.length; i++){
- if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
- isPerpermissionForAllGranted=true;
- }else{
- isPerpermissionForAllGranted=false;
- }
- }
- Log.e("value", "Permission Granted, Now you can use local drive .");
- } else {
- isPerpermissionForAllGranted=true;
- Log.e("value", "Permission Denied, You cannot use local drive .");
- }
- if(isPerpermissionForAllGranted){
- shoro();
- }
- break;
- }
- }
- public void shoro(){
- boolean isPermissionGiven = isStoragePermissionGranted();
- Toast.makeText(this,"ok2",Toast.LENGTH_SHORT).show();
- if (isPermissionGiven){
- vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";
- vCard = new ArrayList<String>();
- cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
- if(cursor!=null&&cursor.getCount()>0)
- {
- Toast.makeText(this,"ok3",Toast.LENGTH_SHORT).show();
- cursor.moveToFirst();
- for(int i =0;i<cursor.getCount();i++)
- {
- try {
- get(cursor);
- } catch (Exception e){
- e.printStackTrace();
- }
- Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i));
- cursor.moveToNext();
- }
- }
- else
- {
- Log.d("TAG", "No Contacts in Your Phone");
- }
- }
- }
- public byte[] read(File file) throws IOException {
- byte[] buffer = new byte[(int) file.length()];
- InputStream ios = null;
- try {
- ios = new FileInputStream(file);
- if (ios.read(buffer) == -1) {
- throw new IOException(
- "EOF reached while trying to read the whole file");
- }
- } finally {
- try {
- if (ios != null)
- ios.close();
- } catch (IOException e) {
- }
- }
- return buffer;
- }
- public void get(Cursor cursor) throws IOException {
- Toast.makeText(this,"ok4",Toast.LENGTH_SHORT).show();
- String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
- Uri vCardUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
- AssetFileDescriptor assetFileDescriptor;
- String vcardstring="";
- if (Build.VERSION.SDK_INT >= 24) {
- FileInputStream inputStream=null;
- byte[] buffer = new byte[4096];
- ByteArrayOutputStream ous = null;
- try {
- assetFileDescriptor = this.getContentResolver().openAssetFileDescriptor(vCardUri, "r");
- if (assetFileDescriptor != null) {
- ous = new ByteArrayOutputStream();
- int read = 0;
- inputStream = assetFileDescriptor.createInputStream();
- while ((read = inputStream.read(buffer)) != -1) {
- ous.write(buffer, 0, read);
- }
- }
- } catch (FileNotFoundException e) {
- Log.e(TAG, "Vcard for the contact " + lookupKey + " not found", e);
- } catch (IOException e) {
- Log.e(TAG, "Problem creating stream from the assetFileDescriptor.", e);
- }finally {
- try {
- if (ous != null)
- ous.close();
- } catch (IOException e) {
- }
- try {
- if (inputStream != null)
- inputStream.close();
- } catch (IOException e) {
- }
- }
- vcardstring= new String(ous.toByteArray());
- }else{
- assetFileDescriptor = this.getContentResolver().openAssetFileDescriptor(vCardUri, "r");
- FileInputStream fis = assetFileDescriptor.createInputStream();
- byte[] buf = new byte[(int) assetFileDescriptor.getDeclaredLength()];
- fis.read(buf);
- vcardstring= new String(buf);
- }
- vCard.add(vcardstring);
- Log.i("File Kapil", "path "+getFilesDir().getAbsolutePath().toString());
- String storage_path = getFilesDir().getAbsolutePath() + File.separator + vfile;
- FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
- mFileOutputStream.write(vcardstring.toString().getBytes());
- }
- }
Add Comment
Please, Sign In to add comment