Android : How to File Save (Screenshot) Locally Android Studio?

on Thursday, April 16, 2015


I'm nearing the end of an internship for app development and I was assigned to create an app from scratch, instead of porting the apple version. I am having trouble figuring out how to save an in app screenshot, and then saving it to the device. I do not want to save externally because you can't assume everyone uses an SD card.


Here is my code, when I hit send I want the screenshot generated and saved so that when I use my share intent I can send the screenshot.



import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.text.TextWatcher;
import android.text.Editable;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.content.Intent;
import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends ActionBarActivity {
TextView CodexTV;
EditText CodexET;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Loading our AvianKingdom Codex Font
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/WWAvianKingdom-Regular.ttf");
//Text view label
CodexET = ((EditText) findViewById(R.id.CodexMessage));
//REAL-TIME Textview change
CodexET.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { //Convert the Text to String
String inputText = CodexET.getText().toString();
CodexTV = ((TextView) findViewById(R.id.CustomFontText));
CodexTV.setText(inputText);
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
//Applying the font
CodexET.setTypeface(tf);

//Screenshot our Codex'ed Image
CodexET.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
doShare(getDefaultIntent());
captureScreen();
case KeyEvent.KEYCODE_ENTER:
doShare(getDefaultIntent());
captureScreen();
default:
break;
}
}
return false;
}
});
}

// Basic Share Intent to handle Sharing through Applications.
private Intent getDefaultIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
return intent;
}

private ShareActionProvider mShareActionProvider;

@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);

//Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);

// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Set history different from the default before getting the action
// view since a call to MenuItemCompat.getActionView() calls
// onCreateActionView() which uses the backing file name. Omit this
// line if using the default share history file is desired.
mShareActionProvider.setShareIntent(getDefaultIntent());

// Return true to display menu
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{ // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}

return super.onOptionsItemSelected(item);
}

//Function for Screenshot and Saveing picture.
public void captureScreen()
{ // Image naming and path to include sd card appending name you choose for file.
String fileName = "DR-CODEX";
// create bitmap screen capture.
View v1 = CodexET.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
File imageFile = new File (this.getFilesDir(), fileName);

try
{
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void doShare(Intent shareintent)
{
mShareActionProvider.setShareIntent(shareintent);
invalidateOptionsMenu();
}

0 comments:

Post a Comment