Video Intent not saving video to desired location - android

I have implemented a code to save the captured video to a custom location.
// Constants
final static int REQUEST_VIDEO_CAPTURED = 1;
String CAPTURE_TITLE="MyVideo.3gp";
// Specified the desired location here
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
Now On Activity result I m getting the default path only and not the desired path where i intent to save the video.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
Uri capturedImageUri = data.getData();
Toast.makeText(this, capturedImageUri .getPath(), TOAST.LENGTH_LONG).show();
}
}
Now I dont know why it is not saving it to desired location similar thing I did try with Image capture and it worked.
Also I have added the desired permissions.
Any thoughts!!

try this instead...
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)), CAPTURE_TITLE);

File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
Change To:
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", CAPTURE_TITLE);

Related

java.lang.NullPointerException: uriString when receiving image sd card path

what is the reason of this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.sqlfirst.AddImage}: java.lang.NullPointerException: uriString
the error is pointing at this line where I am receiving the intent
img.setImageURI(Uri.parse(imagePath ));
I am trying to send a sd card path through an intent to another acitivity and convert it into an image
this is the code:
Here sending the path of the image in sd card
public void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
// startActivityForResult(
// Intent.createChooser(intent, "Complete action using"),2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Bundle extras2 = data.getExtras();
filePath = data.getData();
Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
here I suppose to recieve the path of the image and decrease the size of the image.
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView);
img.setImageURI(Uri.parse(imagePath ));
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
bitmap.recycle();
To use the path correctly you should first create it to have somewhere to store it, you can use that and later on delete it or use the data from the result you got.
Here is the code to create a Uri, pass it to your Intent. Once you get the result you can pass the Uri to another class by using getPath from the Uri package.
/**
* Creating file uri to store image/video
*/
public static Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
/**
* returning image / video
*/
private static File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"YOUR DIRECTORY NAME");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("image upload", "Oops! Failed create "
+ "YOUR DIRECTORY NAME" + " directory");
return null;
}
}
//TODO change naming
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
EDIT 1:
To convert a file to a bitmap you can use this code, provided by #Nikhilreddy Gujjula in this question
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
You are setting imagepath as an extra and trying to retrieve imagePath.
Also note that setImageURI() is not a good choice, as it will load and decode the image on the main application thread. Use an image-loading library, like Picasso or Universal Image Loader.

How do I supply file path that is the same as the path in the gallery when taking photo in Android?

I tried to take a picture using camera intent and want to get the file path of the full size image. I am following the tutorial from http://developer.android.com/training/camera/photobasics.html. The problem, when I look into the file path in MediaStore.Images.Media.DATA column using MediaStore.Images.Media.EXTERNAL_CONTENT_URI, the image have different path. How do I supply the same file path? I looked at taking a photo and placing it in the Gallery, I though it is simply just change Environment.DIRECTORY_PICTURES to Environment.DIRECTORY_DCIM, but it still didn't work.
Below is my code for taking photo.
private Uri photoPath;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
getContentResolver().notifyChange(photoPath, null);
System.out.println(photoPath);
}
}
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.camera_button) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
try {
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String image = "IMG_" + timestamp;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photo = File.createTempFile(image, ".jpg", storageDir);
photoPath = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
}
my code above produce file path /storage/emulated/0/Pictures/IMG_20150805_121624.jpg while it is actually /storage/emulated/0/DCIM/100MEDIA/IMAG0531.jpg in the gallery. the former path is really my image (as I specified it on photoPath), but I want to get the later path instead. How could I do that?
You will have to use Environment.DIRECTORY_DCIM.
The android API DOC clearly has the details of various directories which can be used.
I hope this will help you to save your file to /storage/emulated/0/DCIM/.

error while decoding image captured with camera

I am launching the camera to capture a picture, which is stored in a new file in a specific folder, in the external storage.
The problem is that when I try to decode the image from the file using BitmapFactory.decodeFile() in my onActivityResult() method, the returned image is null. Is this because the file is not yet complete (i.e. the process of the camera has not finished to save the image)? How can I solve this problem?
Here is the code with which I am launching the camera:
private void launchCamera() {
//create folder
File imagesFolder = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
Consts.MEDIA_EXTERNAL_FOLDER);
if(!imagesFolder.exists())
imagesFolder.mkdirs();
String fileName = "pic_"+Consts.CAMERA_DATE_FORMAT.format(new Date());
File image = null;
try {
image = File.createTempFile(fileName, ".jpg", imagesFolder);
} catch(IOException e) {
Log.e(TAG, "Error creating new file.");
}
capturedImageURI = Uri.parse(image.getAbsolutePath());
//launch camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageURI);
startActivityForResult(intent, CAMERA_ACTION);
}
And this is when I get the image in the onActivityResult():
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_ACTION:
getContentResolver().notifyChange(capturedImageURI, null);
File f = new File(capturedImageURI.toString());
decodedBitmap = Helper.decodeSampledBitmapFromResource(filePath);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(decodedBitmap);
break;
}
How can I solve this problem?
File, AFAIK, does not understand file:/// URL formats, which is what you will get from capturedImageURI.toString(). Rather than regenerating the File, use the one you already created.

IMAGE_CAPTURE on resultActivity the file doesn't exist yet

This a rare case, i'm trying to capture an image from native camera activity on my samsung galaxy s3 and onActivityResult the file doesn't exist to read yet. If i disconnect the phone and connect again then the i can see the image on my explorer.
It likes that the camera activity writes the file with delay or something...
i have readed all questions and answers and other forums, but it is my problem; My code:
... onCreate
File file = new File( _path );
pathUri = Uri.fromFile( file );
...
protected void startCameraActivity()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, pathUri);
startActivityForResult( intent, CAMERA_REQUEST_CODE );
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
{
// test if exits image
if((new File(_path)).exists())
... do something
}
... continue
if((new File(_path)).exists()) always retunrs FALSE, how is it possible?
Thans in advance
I have never seen this occur in my app. However, this could potentially be a workaround.
Note: Declare the Uri instance initialURI globally so you can use it throughout the Activity. You could also change the name to something that suits your naming convention.
In this, right before the Intent is triggered, I pass a specific path for the Image to be stored at along with the name for the File.
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals
(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
"SOME_FOLDER_NAME/");
else
cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(),
"SOME_FOLDER_NAME/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, ACTION_REQUEST_CAMERA);
And finally, in the onActivityResult():
Note: This I'm guessing should work. My application uses the Aviary SDK and the code for that differs vastly from the conventional work done in this method.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
// USE EITHER THIS
imgView.setImageURI(initialURI);
// OR USE THIS
getContentResolver().notifyChange(initialURI, null);
ContentResolver cr = getContentResolver();
try {
// SET THE IMAGE FROM THE CAMERA TO THE IMAGEVIEW
Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, initialURI);
// SET THE IMAGE USING THE BITMAP
imgvwSelectedImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you do not need the the Image stored on the SD-Card, you could delete it once you are done with it. This I am guessing (guessing because I have never experienced what the OP is facing problem with) this should do it for you.
Oh. Almost forgot. You will need to declare this permission in your Manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Default Camera Activity Not Finishing Upon OK button press

I'm calling the default camera from my activity and then handling the onActivityResult. My code seems to work fine on the LG Ally which doesn't have a confirmation when a picture is taken. However, when I run the same app on the Nexus S, it prompts me with an "Ok", "Retake", or "Cancel" before returning to my activity. While "Cancel" works, returning to my activity without saving the picture, "Ok" doesn't seem to have any effect, not even returning to my activity.
My code below:
private void captureImage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + (new UserContextAdapter(this)).getUser() + "/");
path.mkdirs();
File file = new File(path, "Image_Story_" + mRowId.toString() + ".jpg");
newImageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);
startActivityForResult(intent, CAPTURE_IMAGE);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case CAPTURE_IMAGE:
switch (resultCode ) {
case 0:
Log.i("CAPTURE", "Cancelled by User");
break;
case -1:
mImageUri = newImageUri;
setImageFromUri();
}
}
I think I just had the exact same problem.
If the path to save the picture isn't correct, the camera won't return to your app. Once I made sure the directory exists, everything worked fine. Make sure the directory exists, then it should work.
-- Edit --
I just saw, that you call path.mkdirs(); but I think that it doesn't work. As you can read in the android doc "Note that this method does not throw IOException on failure. Callers must check the return value.". Please be sure to check if the directory really exists.
hth
Also, make sure your application has <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> if you're using Environment.getExternalStorageDirectory().getPath() above.
Hope this helps =)
please check this
Case 1:
Uri newImageUri = null;
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/");
path.mkdirs();
boolean setWritable = false;
setWritable = path.setWritable(true, false);
File file = new File(path, "Image_Story_" + System.currentTimeMillis() + ".jpg");
newImageUri = Uri.fromFile(file);
Log.i("MainActivity", "new image uri to string is " + newImageUri.toString());
Log.i("MainActivity", "new image path is " + newImageUri.getPath());
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
Case 2:
String fileName = "" + System.currentTimeMillis() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Log.i("MainActivity", "new image uri to string is " + imageUri.toString());
Log.i("MainActivity", "new image path is " + imageUri.getPath());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
I am able to save images through camera on nexus s in both of the above cases
In case 1:
a.Image is stored in custom folder.
b. If the “System.currentTimeMillis()” is changed to (“new Date().toString()”) image is not saved and camera does not return to my activity.
(Probably because “System.currentTimeMillis” has no spaces and “new Date().toString()” might be having some special characters and spaces)
In case 2:
a. Image is stored in camera folder
Thanks to all
I have same problem.You have to do only one job,in your Phone storage check that you have Pictures directory or not.If you don't have such library then make it manually.
Hope this works for you.

Categories

Resources