Android Open Source - Sketcher File Helper






From Project

Back to project page Sketcher.

License

The source code is released under:

Apache License

If you think the Android project Sketcher listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.sketcher;
/*from  w w  w . j ava  2s. co m*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;

public class FileHelper {
  private static final String FILENAME_PATTERN = "image_%d.png";

  private final Sketcher context;
  boolean isSaved = false;

  /* package */FileHelper(Sketcher context) {
    this.context = context;
  }

  private File getSDDir() {
    File file = new File(Environment.getExternalStorageDirectory(),
        "sketcher");
    if (!file.exists()) {
      file.mkdirs();
    }

    return file;
  }

  Bitmap getSavedBitmap() {
    if (!isStorageAvailable()) {
      return null;
    }

    File lastFile = getLastFile(getSDDir());
    if (lastFile == null) {
      return null;
    }

    Bitmap savedBitmap = null;
    try {
      FileInputStream fis = new FileInputStream(lastFile);
      savedBitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
    return savedBitmap;
  }

  File getLastFile(File dir) {
    int suffix = 1;

    File newFile = null;
    File file = null;
    do {
      file = newFile;
      newFile = new File(dir, String.format(FILENAME_PATTERN, suffix));
      suffix++;
    } while (newFile.exists());

    return file;
  }

  private File getUniqueFilePath(File dir) {
    int suffix = 1;

    while (new File(dir, String.format(FILENAME_PATTERN, suffix)).exists()) {
      suffix++;
    }

    return new File(dir, String.format(FILENAME_PATTERN, suffix));
  }

  private void saveBitmap(File file) {
    try {
      FileOutputStream fos = new FileOutputStream(file);
      Bitmap bitmap = context.getSurface().getBitmap();
      if (bitmap == null) {
        return;
      }
      bitmap.compress(CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  private boolean isStorageAvailable() {
    String externalStorageState = Environment.getExternalStorageState();
    if (!externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
      Toast.makeText(context, R.string.sd_card_is_not_available,
          Toast.LENGTH_SHORT).show();
      return false;
    }
    return true;
  }

  void share() {
    if (!isStorageAvailable()) {
      return;
    }

    new SaveTask() {
      protected void onPostExecute(File file) {
        isSaved = true;

        Uri uri = Uri.fromFile(file);

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("image/png");
        i.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(i,
            context.getString(R.string.send_image_to)));

        super.onPostExecute(file);
      }
    }.execute();
  }

  void saveToSD() {
    if (!isStorageAvailable()) {
      return;
    }

    new SaveTask().execute();
  }

  File saveBitmap() {
    File newFile = getUniqueFilePath(getSDDir());
    saveBitmap(newFile);
    notifyMediaScanner(newFile);
    return newFile;
  }

  private void notifyMediaScanner(File file) {
    Uri uri = Uri.fromFile(file);
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
        uri));
  }

  private class SaveTask extends AsyncTask<Void, Void, File> {
    private ProgressDialog dialog = ProgressDialog.show(context, "",
        context.getString(R.string.saving_to_sd_please_wait), true);

    protected File doInBackground(Void... none) {
      context.getSurface().getDrawThread().pauseDrawing();
      return saveBitmap();
    }

    protected void onPostExecute(File file) {
      dialog.dismiss();

      String absolutePath = file.getAbsolutePath();
      String sdPath = Environment.getExternalStorageDirectory()
          .getAbsolutePath();
      String beautifiedPath = absolutePath.replace(sdPath, "SD:/");

      Toast.makeText(
          context,
          context.getString(R.string.successfully_saved_to,
              beautifiedPath), Toast.LENGTH_LONG).show();

      context.getSurface().getDrawThread().resumeDrawing();
    }
  }

}




Java Source Code List

org.sketcher.AboutDialog.java
org.sketcher.Controller.java
org.sketcher.FileHelper.java
org.sketcher.HistoryHelper.java
org.sketcher.SketcherApplication.java
org.sketcher.Sketcher.java
org.sketcher.Style.java
org.sketcher.Surface.java
org.sketcher.colorpicker.AlphaPicker.java
org.sketcher.colorpicker.HuePicker.java
org.sketcher.colorpicker.PickerDialog.java
org.sketcher.colorpicker.Picker.java
org.sketcher.colorpicker.PreviewView.java
org.sketcher.colorpicker.SatValPicker.java
org.sketcher.colorpicker.Utils.java
org.sketcher.style.ChromeStyle.java
org.sketcher.style.CirclesStyle.java
org.sketcher.style.EraserStyle.java
org.sketcher.style.FurStyle.java
org.sketcher.style.GridStyle.java
org.sketcher.style.LongfurStyle.java
org.sketcher.style.RibbonStyle.java
org.sketcher.style.ShadedStyle.java
org.sketcher.style.SimpleStyle.java
org.sketcher.style.SketchyStyle.java
org.sketcher.style.SquaresStyle.java
org.sketcher.style.StylesFactory.java
org.sketcher.style.WebStyle.java