Sunday, April 17, 2011

Share via Email Intent in Android (Image attachment)

Here is the code for Sharing via Email functionality, and you can attach image with the email.

//cacheDir is file path to your application's cache directory where image is located.
//You can provide your own file object by replacing object f.
File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), getString(getApplicationInfo().labelRes));
File f = new File(cacheDir, "image_name.jpg");


Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/jpeg"); intent.putExtra(Intent.EXTRA_TEXT, "Email body over here"); intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject over here"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); startActivity(Intent.createChooser(intent, "Share via:"));

Friday, April 15, 2011

Custom text linkify in android

Here is the demo for custom text linkify in android.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.text.util.Linkify.TransformFilter;
import android.widget.TextView;

public class LinkifyTextDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView textView = (TextView)findViewById(R.id.txtSampleTextView);
textView.setText("This is a demo of custom text linkify, developed by @Bharat");

MatchFilter matchFilter = new MatchFilter() {
public final boolean acceptMatch(CharSequence s, int start, int end) {
// you can compare match over here
// return s.toString().equals("@Bharat");
return true;
}
};
TransformFilter transformFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return "www.android-geek.blogspot.com/2011/04/linkify-text-in-android.html";
}
};

Pattern pattern = Pattern.compile("@Bharat");
String scheme = "http://";
Linkify.addLinks(textView, pattern, scheme, matchFilter, transformFilter);
}
}

screenshoot: