Photo not saved in location given - android

I am trying to take a picture in my app, and then redisplay it. When I start the camera intent, I pass a URI to save the photo location in. Here is how I create the file:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "Waypoint_photo_" + timeStamp + ".jpg";
File storageDir = new File(PICTURE_FOLDER);
storageDir.mkdirs();
Log.v("FILE", String.format("New file: %s%s%s", PICTURE_FOLDER, File.separator, imageFileName));
return new File(storageDir, imageFileName);
Where picture folder is:
public static final String PICTURE_FOLDER = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + "UtopiaWaypoints";
This creates a file similar to:
/storage/emulated/0/Pictures/UtopiaWaypoints/Waypoint_photo_20150718_102116.jpg
In my onActivityResult, I get the file from the same path, and create a thumbnail version of it. At this point if I look on the device I see the files, but they are in /mnt/shell/emulated/0/Pictures/UtopiaWaypoints:
shell#shamu:/mnt/shell/emulated/0/Pictures/UtopiaWaypoints $ ls
Waypoint_photo_20150718_102825.jpg
Waypoint_photo_20150718_102825.png
shell#shamu:/mnt/shell/emulated/0/Pictures/UtopiaWaypoints $ pwd
/mnt/shell/emulated/0/Pictures/UtopiaWaypoints
On my device (nexus 6), /storage/emulated exists (neither is a symbolic link), but it only contains a "legacy" folder.
So when I try to load the file later from the /storage/emulated directory (which is what the Environment.PICTURE_DIRECTORY returns, the app can't find the file.
// This line outputs something similar to: /storage/emulated/0/Pictures/UtopiaWaypoints/Waypoint_photo_20150718_1042581116411973.png
Log.v("WPA", String.format("Loading thumbnail from: %s", Utils.getFullThumbnailPath(wayPoint.getPicture())));
Bitmap imageBitmap = BitmapFactory.decodeFile(Utils.getFullThumbnailPath(wayPoint.getPicture()));
wayPointImageView.setImageBitmap(imageBitmap);
The second to the last line (decodeFile) shows this in the log:
D/skia﹕ --- SkImageDecoder::Factory returned null
imageBitmap is null and the ImageView is blank.
What am I doing wrong?

Related

Saving file in sd card without extension and access that with extension

I am downloading a pdf file from server and saving it on sd card without extension (for security purpose so that normal user can't open that file from file manager).For e.g.- I am downloading abc.pdf and saving it on sd card with abc on below path
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "files");
And later I want to view that file by adding extension.
So when I am accessing that file from code by using below code then its gives error file does't exist..
String s=Environment.getExternalStorageDirectory() + "/files/" + "abc";
File pdfFile = new File(s+".pdf");
So how can I save file in sd card without extension and later open that file with extension.
Workaround for your problem is to rename your saved pdf file without extension to filename with .pdf extension & after completing/accessing view or read operation on your new pdf extension file again you can rename it to file without extension
Example Usage:
String currentFileName = Environment.getExternalStorageDirectory() + File.separator
+ "files" + File.separator + "abc";
String renamedFilename = currentFileName + ".pdf";
boolean isRenamed = renameFile(currentFileName, renamedFilename);
Log.d("isRenamed: ", "" + isRenamed);
if(isRenamed {
//perform your operation
}
Again rename the file if not in use (here exchange renameFile() parameters):
boolean renameAfterOperation = renameFile(renamedFilename, currentFileName);
Log.d("renameAfterOperation : ", "" + renameAfterOperation );
And here is renameFile(currentFilePath, renamedFilePath):
public boolean renameFile(String currentFilePath, String renamedFilePath){
File currentFile = new File(currentFilePath);
File newFile = new File(renamedFilePath);
boolean isrenamed = currentFile.renameTo(newFile);
return isrenamed;
}
In this way whenever you want to perform operation on saved file first rename it to .pdf & whenever it's not in use, again rename it without extension. Let me know if this works for you..

Single filename for captured images, overwriting the previous image, i.e.."TestPic.jpg"

Each time I take a photo, I want the image name to be "TestPic.jpg" every time, but every time I click to take a pic, it saves as: TestPic2203 , TestPic4023, etc.
I wrote code to delete the previous image and keep only one image in that folder which is replaced by next picture.
public static File CreateImageFile() throws IOException {
String ImageFileName = "TestPic";
File storageDir = new File(Environment.getExternalStorageDirectory()+"/EasyRecharge");
if(!storageDir.exists()){
File EasyRechargeDir = new File(Environment.getExternalStorageDirectory().getPath()+"/EasyRecharge/");
EasyRechargeDir.mkdirs();
}
File test = new File(Environment.getExternalStorageDirectory().getPath()+"/EasyRecharge/TestPic.jpg");
if(test.exists()){
test.delete();
}
File image = File.createTempFile(ImageFileName,// prefix
".jpg", // suffix
storageDir
);// directory
AppLog.showAppFlow("file created");
AppLog.showAppFlow("image filename:"+image);
return image;
}
If you want the file name to be the same each time, don't use File.createTempFile(). It's purpose is exactly to create a unique name for each file...
Instead use:
File image = new File(storageDir, ImageFileName + ".jpg");
This way, the filename will be the same each time.

Captured video being saved to different folder than specified

I'm making an app that let's you either capture photo/video, or choose an existing photo/video before sending. I set up the directory to save the files in here:
String appName = Main.this.getString(R.string.app_name);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), appName);
and I name the files here
File mediaFile;
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath() + File.separator;
if(mediaType == MEDIA_TYPE_PHOTO){
mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
} else if(mediaType==MEDIA_TYPE_VIDEO){
mediaFile = new File(path + "VID_" + timestamp + ".mp4");
}
So it's saving my pictures under /storage/emulated/0/pictures/(app name) with correct timestamped format. However, my videos are being saved to /storage/emulated/0/DCIM/100MEDIA and are just being named VIDEO0073, VIDEO00074, etc. I tried changing the directory name to MOVIES instead of PICTURES or DCIM, but there is no effect. I'm on an HTC One running Android 4.3
This is a bug that occurs on certain devices including the HTC One. If you include your code where you declare your video Intent I could more precisely answer the question, but basically, after you declare your Intent for the video (not image), get your Uri like so:
videoUri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new ContentValues());
In this case the video will be saved in the /storage/emulated/0/video folder instead of the pictures or dcim folders you mentioned above.

Trouble with adding file path to the MediaStore.ACTION_IMAGE_CAPTURE intent

I am adding a photo from the phones camera to a Fragment. I am including the file path to where I would like the pic to get saved in the camera intent(MediaStore.ACTION_IMAGE_CAPTURE).
My problem is that after launching the camera and taking the pic the I am not able to get back to my app. The screen stays in the part where the user gets the option to accept the pic or take another one. The phone doesn't hang or crash and I am able to get back to my app by hitting the back button, but when I get back to the app it is without the pic.
I hope I have made my problem clear, if not let me know and I will try to explain better. I have attached (what I think) are the relevant bits of code.Let me know if you need to see anymore of the code.
Launching the camera intent:
void takePic()
{
if(isExternalStorageWritable() && isExternalStorageReadable())
{
Log.w("Rakshak", "in the take pic");
File file = getPicStoragePath();
Uri uriSavedImage=Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, CAMERA_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
public File getPicStoragePath() {
Log.w("Rakshak", "in the get pic file");
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + File.separator +"YAN");
if(!dir.exists())
dir.mkdirs();
File file = new File(dir,getPicName());
return file;
}
public String getPicName()
{
Log.w("Rakshak", "in the get pic name");
if(title.getText().toString().trim().length() == 0)
{
Log.w("Rakshak", "no title");
Calendar cal=Calendar.getInstance();//get an instance of the calendar
return String.format("%1$te %1$tB %1$tY,%1$tI:%1$tM:%1$tS %1$Tp",cal);// get the data/time when the note was created
}
else
{
return title.getText().toString().trim();
}
}
I have these permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The relavent bits from the onActivityResult:
case CAMERA_IMAGE_ACTIVITY_REQUEST_CODE:
Log.w("Rakshak", "in the camera case");
myBitmap = data.getExtras().getParcelable("data");
photo.setVisibility(View.VISIBLE);
photo.setImageBitmap(myBitmap);
update_pic = true;
return;
There are no error messages in the LogCat. And I could not find anything of note in there.
I get the pic inserted onto the image view just fine if I dont add a file path to the camera intent. It is only when I add the file path that the stays in the "accept pic" bit of the camera.
Your getPicStoragePath() might be the issue. Try something like this:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}

Move Images to gallery programmatically in Android

This question may have been asked elsewhere but did not work for me.
I save images in the app-private folder and the user has the option to move it to Android gallery. So my question is how to do that?
This is my code
String outPath = "new path which is gallery";
File outFile = new File(outPath);
// File (or directory) to be String
sourcePath="image in the app-private folder";
File file = new File(sourcePath);
// Destination directory
boolean success = file.renameTo(new File(outFile, "name of image"));
System.out.println("success: "+success);
success here is always "false"

Categories

Resources