Skip to content

Instantly share code, notes, and snippets.

@nikreiman
Created April 5, 2012 11:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikreiman/2310318 to your computer and use it in GitHub Desktop.
Save nikreiman/2310318 to your computer and use it in GitHub Desktop.
Take a screenshot of your Android app and email it somewhere (no root access required)
public static void sendFeedbackScreenshot(final Activity activity) {
try {
final View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = rootView.getDrawingCache();
// Activity.getCacheDir() won't work here, because the email intent can't
// access your app's internal storage. So you need to find a good temporary
// location in SD card storage.
File outputDir = new File(android.os.Environment.getExternalStorageDirectory(), "tmp");
File outputFile = new File(outputDir, "screenshot.png");
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
rootView.setDrawingCacheEnabled(false);
startEmailActivity(activity, Uri.fromFile(outputFile));
}
catch(Exception e) {
// Show error message, etc.
}
}
public static void startEmailActivity(Context context, Uri attachment) {
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{TO_EMAIL_ADDRESS});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, YOUR_EMAIL_SUBJECT);
sendEmailIntent.putExtra(Intent.EXTRA_STREAM, attachment);
context.startActivity(Intent.createChooser(sendEmailIntent, "Send email"));
}
@ajaybarvey
Copy link

Sir, Thanks for posting this code. Can you share the complete android project code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment