Created
July 4, 2013 10:02
-
-
Save damarnez/5926471 to your computer and use it in GitHub Desktop.
photo helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Environment; | |
import android.provider.MediaStore; | |
import android.util.Log; | |
public class Photo { | |
Activity context; | |
Uri fileUri; | |
public static final int ID = 303; | |
public Photo(Activity context) { | |
this.context = context; | |
} | |
public Intent makePhotoIntent(){ | |
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE ); | |
fileUri = getOutputMediaFileUri(); | |
intent.putExtra( MediaStore.EXTRA_OUTPUT, fileUri ); | |
return intent; | |
} | |
int getLastImageId(){ | |
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA }; | |
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC"; | |
Cursor imageCursor = context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy); | |
if(imageCursor.moveToFirst()){ | |
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID)); | |
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); | |
Log.d("Photo", "getLastImageId::id " + id); | |
Log.d("Photo", "getLastImageId::path " + fullPath); | |
imageCursor.close(); | |
return id; | |
}else{ | |
return 0; | |
} | |
} | |
public Uri getOutputMediaFileUri(){ | |
return Uri.fromFile(getOutputMediaFile()); | |
} | |
/** Create a File for saving an image or video */ | |
private File getOutputMediaFile(){ | |
// To be safe, you should check that the SDCard is mounted | |
// using Environment.getExternalStorageState() before doing this. | |
File mediaStorageDir = null; | |
if (!Environment.isExternalStorageRemovable()){ | |
mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( | |
Environment.DIRECTORY_PICTURES), "MyCameraApp"); | |
}else{ | |
mediaStorageDir = context.getDir(Environment.DIRECTORY_PICTURES, Context.MODE_PRIVATE); | |
} | |
// 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()){ | |
Log.i("MyCameraApp","No existe la carpeta"); | |
if (! mediaStorageDir.mkdirs()){ | |
Log.i("MyCameraApp", "failed to create directory"); | |
return null; | |
} | |
} | |
// Create a media file name | |
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); | |
// File mediaFile = new File(mediaStorageDir.getPath() + File.separator +); | |
File image = null; | |
try { | |
image = File.createTempFile("IMG_"+ timeStamp , "jpg",mediaStorageDir); | |
} catch (IOException e) { | |
Log.d("MyCameraApp","Error create image"); | |
e.printStackTrace(); | |
} | |
return image; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment