Android Bitmap Save saveImage(String filename, Bitmap bm)

Here you can find the source of saveImage(String filename, Bitmap bm)

Description

Save the image as file

License

Open Source License

Parameter

Parameter Description
filename The file
bm The bitmap

Return

The file path

Declaration

public static String saveImage(String filename, Bitmap bm) 

Method Source Code

/* Copyright (c) 2010-2014 ARTags Project owners (see http://www.artags.org)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.//from   ww w . j a  v a  2  s .  c om
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import org.artags.android.app.apilevels.ApiLevel4;
import org.artags.android.app.apilevels.ApiLevels;

public class Main{
    private static final String ROOT_DIRECTORY = "/ARTags";
    /**
     * Save the image as file
     * @param filename The file
     * @param bm The bitmap
     * @return The file path
     */
    public static String saveImage(String filename, Bitmap bm) {
        return BitmapUtil.saveImage(filename, bm, false);
    }
    /**
     * Save the image as file
     * @param filename The file
     * @param bm The bitmap
     * @param landscape The orientation
     * @return The file path
     */
    public static String saveImage(String filename, Bitmap bm,
            boolean landscape) {
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()) {
            if (landscape) {
                Matrix matrix = new Matrix();
                matrix.postRotate(-90);
                bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                        bm.getHeight(), matrix, true);
            }
            try {
                File directory = new File(root.getPath() + ROOT_DIRECTORY);
                if (!directory.exists()) {
                    directory.mkdir();
                }
                String filepath = directory.getPath() + "/" + filename;
                File file = new File(filepath);
                FileOutputStream fos = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 0, fos);
                fos.close();

                return filepath;

            } catch (Exception e) {
                Log.e("ARTags:BitmapUtil:saveImage",
                        "Exception while writing the tag", e);
            }
        } else {
            Log.e("ARTags:BitmapUtil:saveImage",
                    "Can't write on the volume");
        }
        return null;

    }
}

Related

  1. saveBitmap(Bitmap bitmap, File file)
  2. saveBitmap(String bitName, Bitmap bitmap)
  3. saveBitmap(final Bitmap bitmap, final String savePath)
  4. saveBitmapFromScrollView(ScrollView scrollView, String path)
  5. saveBitmaptoFile(Bitmap bmp, String path, CompressFormat format, int quality)
  6. saveImage(String filename, Bitmap bm, boolean landscape)
  7. saveMyBitmap(String bitName, Bitmap mBitmap)
  8. saveToLocal(Bitmap bmp, String fileName)
  9. storeImage(Context context, Bitmap image)