Saving VCard contact via intent - android

I want to save a contact data which is in VCard format in user's contacts via sending intent. Is there any way to do it?
NOTE: I don't want to save VCard data in a .vcf file and then give it's uri to ‍‍intent like the code below.
String scanned = "..." // contact in VCard format
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
File vcfFile = new File(getCacheDir(), "tmp.vcf");
try {
FileOutputStream fos = new FileOutputStream(vcfFile);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(scanned);
osw.close();
fos.close();
i.setDataAndType(Uri.fromFile(vcfFile), "text/vcard");
startActivity(i);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

If the contact is indeed in the contacts database, you could look at the android contacts app's code, to see how to share a vcard out of it (found from here, inside a file called "QuickContactActivity.java" ) :
private void shareContact() {
final String lookupKey = mContactData.getLookupKey();
final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, shareUri);
// Launch chooser to share contact via
final CharSequence chooseTitle = getText(R.string.share_via);
final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
try {
mHasIntentLaunched = true;
ImplicitIntentsUtil.startActivityOutsideApp(this, chooseIntent);
} catch (final ActivityNotFoundException ex) {
Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
}
}

Related

How to show PDF from Base64 String on Android?

I am developing an Android app. I keep a PDF data in MYSQL database as blob type. I am sending as base64 to Android app. How can I show the pdf in Android app?
As you have the yourBase64String with you, you can convert it to byte array and then save it as file.
FileOutputStream fos = null;
try {
if (yourBase64String != null) {
fos = context.openFileOutput("myPdf.pdf", Context.MODE_PRIVATE);
byte[] decodedString = android.util.Base64.decode(yourBase64String , android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}
} catch (Exception e) {
} finally {
if (fos != null) {
fos = null;
}
}
Now after this open this PDF file
Uri path = Uri.fromFile(new File(myPdf.pdf));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
Add this permission in your manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

How to share a image from its url

I am developing an android application and i need to share the image on button click.But i am getting Image URl only. So, how can i share the image???
And i am getting empty attachment if i give image URL to the intent.
my code is:
sharebut =(Button)findViewById(R.id.sharebut);
sharebut.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
String screenshotUri = flag;
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
Add the path where your image is located in sd card in Uri.parse("file:///"+ yourImagePath)
Use :
String path= "/Downloads/image1.jpg"; //Add your path here
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+ path));
Please try this solution for share image via email from URL.
String path = "";
URL url;
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, description);
try {
url = new URL(thumnbnailURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap immutableBpm = BitmapFactory.decodeStream(input);
Bitmap mutableBitmap = immutableBpm.copy(
Bitmap.Config.ARGB_8888, true);
View view = new View(VideoDetailsActivity.this);
view.draw(new Canvas(mutableBitmap));
path = Images.Media.insertImage(getContentResolver(),
mutableBitmap, "Nur", null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(path);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
I found the solution.. :)
Just created a file and share the content in imageview.
sharebut =(Button)findViewById(R.id.sharebut);
sharebut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgflag.buildDrawingCache();
Bitmap bmap = imgflag.getDrawingCache();
OutputStream out = null;
String path =Environment.getExternalStorageDirectory().toString();
File file = new File(path, "test.png");
try {
file.createNewFile();
out = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Your Image"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

write sqlite data to html file and email html file as attachment

In my application,I have to send sqlite data in Html file as report,means I have write sqlite data to html file,after that this html file should be attach as attachment in email.right now I am sending sqlite data using emailIntent.putExtra method Htmlf format.But this should go as message to mail.but I want some file like html to save on my device.or create new html file and write sqlite data to it,and send as attachment.
Use the code: just take the sql data and store as String and write it using OutputStreamWriter and add the file as an attachment
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String data = "<html><body> My name is John.</body></html>";
String uri = Environment.getExternalStorageDirectory()+"";
File f = new File(uri,"san.html");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fout;
try {
fout = openFileOutput(uri,MODE_WORLD_WRITEABLE);
OutputStreamWriter osw = new OutputStreamWriter(fout);
osw.write(data);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent emailintent = new Intent(Intent.ACTION_SEND);
emailintent.setType("text/html");
emailintent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(f));
emailintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailintent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(f));
emailintent.putExtra(Intent.EXTRA_TEXT, "Enjoy the Attachments");
startActivity(Intent.createChooser(emailintent, "Email:"));
}
}

Unable to attach audio file to Samsung Devices

I unable to attach Audio file to Some of the Samsung Devices like Samsung GalaxyS2,Nexus etc.I don't know whats the problem.I am able to attach all other devices.Please someone help me for my this issue.My code is as:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("sms_body",
getResources().getText(R.string.Message));
sendIntent.setClassName("com.android.mms",
"com.android.mms.ui.ComposeMessageActivity");
AssetManager mngr = getAssets();
InputStream path = null;
try {
path = mngr.open("*.mp3");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(path, 1024);
// get the bytes one by one
int current = 0;
//
try {
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
} catch (IOException e) {
// /TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bitmapdata = baf.toByteArray();
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "*.mp3");
// if (file.exists() == true) {
// } else {
// Log.v("directory is created", "new dir");
// file.mkdir();
// }
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
final File file1 = new File(Environment
.getExternalStorageDirectory().getAbsolutePath(),
"*.mp3");
Uri uri = Uri.fromFile(file1);
Log.e("Path", "" + uri);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("audio/mp3");
// sendIntent.putExtra(Intent.EXTRA_STREAM, mms_uri.toString());
startActivity(Intent.createChooser(sendIntent, ""));
// startActivity(sendIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!", Toast.LENGTH_LONG)
.show();
e.printStackTrace();
}
Don't set the class name explicitly by calling setClassName(). Instead just set the action, type and extra as described here.
You are assuming that there is ExternalStorage i.e. sd card available on the device. Your S2 or Nexus probably does not have the sd card. I have seen this issue before.

Android Read-Only File System Error

Hi I have a gallery in my app which shows the image in an image view when you click on the thumbnail. I am setting up a Long Click Listener for an alert dialog that has 2 buttons one to set as wallpaper and one to share. I have the share intent and the get drawing cache somewhat working. It works in the emulator but not on my phone. I have used many of the examples on this site to get this going but its not working at all. it keeps force closing the app on my phone. any help is appreciated.
alertDialog.setButton2("Share",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
imgView.setDrawingCacheEnabled(true);
Bitmap b = imgView.getDrawingCache();
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + "image.jpg");
try {
file.createNewFile();
OutputStream fos = null;
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Save Example", "Image saved.");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("file:///sdcard/image.jpg");
shareIntent.setData(phototUri);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
});
alertDialog.show();
return true;
}
});
Update: it seems to be a problem with
b.compress(CompressFormat.JPEG, 95, fos);
it keeps saying null pointer.
Turns out the null pointer is actually a read_only error..So its not actually writing the file i get a ioexception read-only file system. Why is it doing this?
ok here s what i ended up doing to get this to share the pictures in case others need it. It turns out my ImageView was not being rendered as a BitMap so it was make files with nothing in them. So instead i am just calling the drawable from its postion and saving that. Its a lot cleaner code for this simple gallery. I have it in an alert dialog but it can be set to a regular button also.
alertDialog.setButton2("Share",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
long currentTime = System.currentTimeMillis();
imgView.setDrawingCacheEnabled(true);
String newFolder = "/CFW";
String extStorageDirectory = Environment
.getExternalStorageDirectory()
.toString();
File sdCard = new File(extStorageDirectory
+ newFolder);
sdCard.mkdir();
File file = new File(sdCard.getAbsolutePath()
+ "/cfw" + currentTime + ".jpg");
try {
InputStream is = getResources()
.openRawResource(pics[position]);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Save Example", "Image saved.");
Intent shareIntent = new Intent(
Intent.ACTION_SEND);
Uri photoUri = Uri
.parse("file:///sdcard/CFW/cfw"
+ currentTime + ".jpg");
shareIntent.setData(photoUri);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM,
photoUri);
startActivity(Intent.createChooser(shareIntent,
"Share Via"));
}
});
alertDialog.show();
return true;
}
});
}

Categories

Resources