Android filepath photo ends up with file:/// - android

I'm trying to get the right filepath to an image I'm taking with the camera. I get the right filepath but it ends up with
file:///storage/emulated/0/Pictures/picture.jpg
when i want it to end up as
storage/emulated/0/Pictures/picture.jpg Any clues on how ti fix this?
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File pictureDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), pictureName);
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pictureDirectory));
imageToUploadUri = Uri.fromFile(pictureDirectory);
//this ends up as file:///
filepath = imageToUploadUri.toString();

Try:
filepath = imageToUploadUri.getPath();
or
File f = new File(imageToUploadUri.getPath());
filepath = f.getAbsolutePath();

Related

Retrieving camera intent image full image uri

I've found ways that have supposedly worked for people to retrieve the uri/bitmap of the camera taken image.
But when I try and create a bitmap out of the non-null uri that I'm getting the file isn't created and doesn't exist.
this the the function which I use to open the camera intent(Which is copied from stackoverflow):
private void openBackCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
currentImageBase64 = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(currentImageBase64);
Log.d("2","file 1: " +file.exists());
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext(),"com.mydomain.fileprovider",file);
currentImageBase64 =outputFileUri+"";
Log.d("2",outputFileUri+" is the uri got from camera");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
and this is how I handel my images:
Log.d("2","camera request!"+ currentImageBase64);
File imgFile = new File(currentImageBase64);
if(imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivPreview.setImageBitmap(myBitmap);
}else{
Log.d("2","file does not exist");
}
note that the camera request log shows that currentImagesBase64 is not null and has a value "content://com.mydomain.fileprovider/name/Pictures/20190121_173332.jpg"
the file anyway, does not exist.
/**
edit: the problem was insufficent permissions, camera and read&write permissions were requested and the problem was resolved!
**/
Turn out Permissions were the problem, once I gave runtime camera read&write storage permissions the problem was fixed!

Android how to create a new file for each download?

I am creating an app where every time press the download button it's create a pdf file in my storage.My problem is newly created file overwrites the existing file with same file name.I need to download with new filename.What i am trying
dirpath =
android.os.Environment.getExternalStorageDirectory().toString();
// int increase=0;
file = new File(dirpath+"/NewPDF.pdf");
if(file.exists()){
increase++;
file = new File(dirpath + "/NewPDF" +increase+".pdf");}
This above lines of program create files but i need to open the last download file
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf"));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
These above lines of code generate an error.
Error:File no longer exist,the file maybe deleted or changed or
removed by another program
1.create a new file and prevent overwiting whenever i click the button
2.open the last downloaded file
I am new to Android and little confused about that and kindly need some help from you guys.
You call Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf")); for open file, so why you added /NewPDF.pdf because its already in your file. just remove it.
Replace this line
Uri uri = Uri.fromFile(new File( file,"/NewPDF.pdf"));
to
Uri uri = Uri.fromFile(new File( file));
You are assigning the same name to every file you create. Try giving a unique name like this:
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
the randomUUIDString will be your unique string for the pdf files. Also, store these strings in a SQLite db if you wish to use them.
The path you have entered in intent not matching with the created file path.
Try this.
dirpath = android.os.Environment.getExternalStorageDirectory().toString();
String lastFilePath = null;
// int increase=0;
file = new File(dirpath+"/NewPDF.pdf");
if(file.exists()){
increase++;
file = new File(dirpath + "/NewPDF" +increase+".pdf");}
lastFilePath = file.getAbsolutePath();
Then you can open the file like this.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(lastFilePath));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Hope it helps:)
Replace if with while and display Intent:
dirpath = android.os.Environment.getExternalStorageDirectory().toString();
int increase = 0;
file = new File(dirpath+"/NewPDF.pdf");
while(file.exists()) {
increase++;
file = new File(dirpath + "/NewPDF" +increase+".pdf");
}
file.createNewFile();
... store data in file here ...
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Use Time stamp instead of Counter, so that you can create a new file every time.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date timeStamp = new Date();
dirpath =
android.os.Environment.getExternalStorageDirectory().toString();
// int increase=0;
file = new File(dirpath+"/NewPDF.pdf");
if(file.exists()){
increase++;
file = new File(dirpath + "/NewPDF" +formatter.format(timeStamp )+".pdf");}

How to save captured photo in application directory in Android

Hello I want to save captured photo in application directory where SQLite etc reside. I don't want to make any folder and any hidden file in the SD Card. How this can be save the captured photo in application directory.
I am trying this below but it is saving in the SD card that is hidden file but I don't want this approach.
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String capturedPhotoName = "." + System.currentTimeMillis() + ".png";
File photo = new File(Environment.getExternalStorageDirectory(),
capturedPhotoName);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, CAMERA_INTENT_REQUEST);
Thanks in advance !
Try this method to save captured image in application storage
public Uri setImageUri() {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File file = new File(directory,System.currentTimeMillis() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
Image will store at this location
/data/data/com.your.packagename/app_data/imageDir
get URI from onActivityResult() and convert it into bytes store bytes in Database.
byte[] UserPic = null;
UserPic = Base64.decode(result.getData(), 0);
save userPic in DataBase

Get uri of picture taken by camera

When the user takes a picture with the camera, I want the image displayed in an ImageView and I want to save the Uri to an online database. I heard saving the Uri of a picture is better than saving the path.
So this is how I start the camera:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile(getActivity())));
String a = String.valueOf(Uri.fromFile(getFile(getActivity())));
intent.putExtra("photo_uri", a);
startActivityForResult(intent, PICK_FROM_CAMERA);
where
private File getFile(Context context){
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/Myapp");
if (!directory.exists()) {
directory.mkdirs();
}
String filename = "bl" + System.currentTimeMillis() + ".jpg";
File newFile = new File(directory, filename);
return newFile;
}
In onActivityResult:
Bundle extras = data.getExtras();
String photo_uri = extras.getString("photo_uri"); //null
This is always null. Btw before sending the intent the Uri looks like file://... instead of content:// which is the uri when I open an image from the gallery. I don't know if that's a problem.
Also, should I save the path instead of the Uri to the database? I read that the Uri can be complicated in certain phones or Android versions. When I let the user select an image from the gallery, I save the Uri to the database, so I think the best way is saving the Uri in this case as well.
I tried out many variations, but I get null every time I try to pass a variable with the intent...
After starting the intent:
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentPicture,PICK_FROM_CAMERA);
In onActivityResult:
case PICK_FROM_CAMERA:
Uri selectedImageUri = data.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
break;
That's all.

How to create a file using content?

After capturing a photo I'm receiving an Intent data from which I'm retrieving an Uri:
Uri contentURI = data.getData();
content Uri is content://media/external/images/media/5576
Using this contentURI I need to save a File to SD card, please advice how. Thank you very much for assistance.
Why not save the Photo to a folder while you are taking a picture from the Camera using an Intent? That will speed things up a bit. Try this piece of code for example:
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(),"CHANGE_TO_YOUR_FOLDER_NAME/camera");
else
cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory(), "CHANGE_TO_YOUR_FOLDER_NAME/camera/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, 2);

Categories

Resources