I am capturing image via my application and I successfully stored in sdcard folder which I had created for my application. Now I need to retrieve the stored image in list view. Saving the images in sqlite is not an efficient way. Can any one direct me in any other way on how to save and retrieve like using path.
Am capturing picture here:
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
And I have also created the directory in sd card to save my captured images via my application below:
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File (
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME
);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type ==MEDIA_TYPE_IMAGE) {
mediaFile = new File (mediaStorageDir.getPath()+ File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
// TODO Auto-generated method stub
return mediaFile;
}
This link will give you how to get the image path and save it in DatabaseLink
And after retrieving the file path, you'll have to load image from that path.
To load image from file path,this link will help you.
Related
In my app, I need to take a photo, then send it via RestApi with other data. I want to store this photo in a different location to process it in the future. My code:
private void TakePictureAfterScan() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
TAKEN_PHOTO = Uri.fromFile(getOutputMediaFile());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, TAKEN_PHOTO);
startActivityForResult(takePictureIntent, ACTIVITY_CODE_REQUEST_IMAGE_CAPTURE);
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), PICTURE_PHOTO_DIR);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
}
When I set a breakpoint in 'onActivityResult' method (which is empty right now for tests), there are already two .jpg files saved in the device:
In DCIM/Camera folder
In my temporary folder
The problem is, both files have different names. I do not need and do not want files from DCIM/Camera folder. Is there any simple way to save taken a photo only in my temporary folder?
instead of this .
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), PICTURE_PHOTO_DIR);
use this..
File mediaStorageDir = new File(Environment.getExternalStorageDirectory().toString() + "folder name" + ImageName;);
Using intent to take a photo, which is later uploaded to a server.
Image is first saved to sdcard like so:
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
Config.IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed create "
+ Config.IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
The saved image has no exif data. Is it due to the uri? (photos taken with the default camera app has exif data so it has to be an issue in my app).
Edit: Code used to launch default camera app:
/**
* Launching camera app to capture image
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
url stored:
/**
* Here we store the file url as it will be null after returning from camera
* app
*/
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on screen orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
Source: http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/
photos taken with the default camera app has exif data so it has to be an issue in my app
Not necessarily. From the code that you have shown, it would appear to be a bug in that particular camera app. Not all camera app developers test ACTION_IMAGE_CAPTURE very well. Unless you are modifying the image sometime after the picture is taken but before you run your EXIF test, the camera app is not adding EXIF tags to pictures taken via ACTION_IMAGE_CAPTURE. These things happen.
You might install some camera apps and see how they behave with ACTION_IMAGE_CAPTURE. Open Camera, for example, supports ACTION_IMAGE_CAPTURE, though I have not tested its EXIF behavior when started with ACTION_IMAGE_CAPTURE.
Currently i am taking image from camera and gallery . When i take picture from camera then the path is storage/sdcard0/pictures/androidfileupload/.
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"Android File Upload");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
so from the above code i am fetching storage/sdcard0/pictures/android file upload/ .
Now i am taking selected image from gallery so it shows error like storage/sdcard0/pictures/androidfileupload/imagename.jpg is not found .
I have debug the code and i get the path of selected image from gallery content://media/external/images/media/26435 but i want the path like
content://media/external/images/media/imagename.jpg.
so how can i get the path with imagename.jpg.?
I want to save image in a directory, but before I save it I want to reduce the size of the image as I need to Upload it later. How can I resize the image and save it?
private static File saveImage(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
is there any library or inbuilt function to do it?
You need to load the image and resize it.See BitmapFactory and you can use decodeFile() to load the image and resize it with BitmapOptions, and Bitmap class has a method to save the scaled image to disk. See compress
I have an activity that allows you to record a video if you open a dialog and click on an icon.
The problem is that after I stop recording it throws a NullPointerException even though the video is saved properly. According to Log Cat the error is not in my code so I tried to place "checkpoints" in my code and I found out that even the onActivityResult of my activity is executed properly so now I'm out of idea what to do.
Here is the Log Cat:
Code:
these are from my dialog that invokes the camera app
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
((Activity)context).startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
private static Uri getOutputMediaFileUri(int type)
{
return Uri.fromFile(getOutputMediaFile(type));
}
private 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.getExternalStorageDirectory()+"/Movies", "MyCameraApp");
// 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("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + "/" +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + "/" +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
This code was more or less copied from the android developers site.
As I mentioned even the onActivityResult of my activity is executed properly(where I dismiss the dialog) after this.
Try this:
private static File getOutputMediaFile(int type)
{
File mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = "IMG_"+ timeStamp + ".jpg";
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = "VID_"+ timeStamp + ".mp4";
} else {
return null;
}
return new File(mediaStorageDir, mediaFile);
}
The getExternalStorageDirectory method returns a File object, not a string you can append a subdirectory to.
It also wonder if the directory returned by that method would be available to a service. The Android specs say:
On devices with multiple users (as described by UserManager), each
user has their own isolated external storage. Applications only have
access to the external storage for the user they're running as.
LOL!!!! Just realized this question was asked 2 years ago! Did you find the answer yet?? LOL