Android Bitmap Save addImageAsApplication(ContentResolver cr, String name, long dateTaken, String directory, String filename, Bitmap source, byte[] jpegData)

Here you can find the source of addImageAsApplication(ContentResolver cr, String name, long dateTaken, String directory, String filename, Bitmap source, byte[] jpegData)

Description

add Image As Application

Declaration

public static Uri addImageAsApplication(ContentResolver cr,
            String name, long dateTaken, String directory, String filename,
            Bitmap source, byte[] jpegData) 

Method Source Code

//package com.java2s;
import java.io.File;

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

import android.content.ContentResolver;
import android.content.ContentValues;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import android.net.Uri;

import android.provider.MediaStore.Images;
import android.util.Log;

public class Main {
    private static final String TAG = "Utils";
    private static final Uri IMAGE_URI = null;

    public static Uri addImageAsApplication(ContentResolver cr,
            String name, long dateTaken, String directory, String filename,
            Bitmap source, byte[] jpegData) {

        OutputStream outputStream = null;
        String filePath = directory + "/" + filename;
        try {/*from www.  j  a v a 2s.c  o m*/
            File dir = new File(directory);
            if (!dir.exists()) {
                dir.mkdirs();
                Log.d(TAG, dir.toString() + " create");
            }
            File file = new File(directory, filename);
            if (file.createNewFile()) {
                outputStream = new FileOutputStream(file);
                if (source != null) {
                    source.compress(CompressFormat.JPEG, 75, outputStream);
                } else {
                    outputStream.write(jpegData);
                }
            }

        } catch (FileNotFoundException ex) {
            Log.w(TAG, ex);
            return null;
        } catch (IOException ex) {
            Log.w(TAG, ex);
            return null;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Throwable t) {
                }
            }
        }

        ContentValues values = new ContentValues(7);
        values.put(Images.Media.TITLE, name);
        values.put(Images.Media.DISPLAY_NAME, filename);
        values.put(Images.Media.DATE_TAKEN, dateTaken);
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        values.put(Images.Media.DATA, filePath);
        return cr.insert(IMAGE_URI, values);
    }
}

Related

  1. saveMyBitmap(String bitName, Bitmap mBitmap)
  2. saveToLocal(Bitmap bmp, String fileName)
  3. storeImage(Context context, Bitmap image)
  4. saveBitmap(Bitmap bitmap, String filename)
  5. saveBitmap2SD(Bitmap bitmap, Context context, String filePath, String fileName)
  6. saveBitmapToFile(Bitmap bitmap, String path)
  7. save(Bitmap bitmap, String fileName)
  8. saveBitmapToFile(Bitmap bitmap, String filename)
  9. saveBitmap(Bitmap bmp, String path, CompressFormat format)