Back to project page AndroidImageUploader.
The source code is released under:
MIT License
If you think the Android project AndroidImageUploader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.examples.imageupload; //from ww w .j av a 2 s . com import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by kcourville on 7/10/2014. */ public class ImageUtils { public static final int MEDIA_TYPE_IMAGE = 1; public static final int MEDIA_TYPE_VIDEO = 2; private static final String TAG = "ImageUtils.java"; public static String getMediaFilename(int type) { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); if (type == MEDIA_TYPE_IMAGE) { return "IMG_" + timeStamp + ".jpg"; } else if (type == MEDIA_TYPE_VIDEO) { return "VID_" + timeStamp + ".mp4"; } else { return null; } } public static Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } public static File getOutputMediaFile(int type) { // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "SendImage"); // This location works best if you want the created images to be shared // between applications and persist after your app has been uninstalled. // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.d(TAG, "failed to create directory"); return null; } } // Create a media file name return new File( String.format("%s%s%s", mediaStorageDir.getPath(), File.separator, ImageUtils.getMediaFilename(type)) ); } public static long getImageId(ContentResolver resolver, Uri imageUri) { long result = -1; String[] projection = {MediaStore.Images.Media._ID}; Cursor cursor = resolver.query(imageUri, projection, null, null, null); if (cursor.moveToFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); result = cursor.getLong(column_index); } cursor.close(); return result; } public static Bitmap getThumbnailFromUri(ContentResolver resolver, Uri uri) throws IOException { Bitmap result = null; long imageId = getImageId(resolver, uri); if (imageId >= 0) { result = MediaStore.Images.Thumbnails.getThumbnail( resolver, imageId, MediaStore.Images.Thumbnails.MINI_KIND, null ); } return result; } public static Bitmap decodeUri(Context context, Uri uri, final int requiredSize) throws FileNotFoundException { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, o); int width_tmp = o.outWidth , height_tmp = o.outHeight; int scale = 1; while(true) { if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, o2); } }