I am using this code to take the picture from the camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
File storagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/");
if(!storagePath.isDirectory()){
storagePath.mkdirs();
}
File myImage = new File(storagePath,
Long.toString(System.currentTimeMillis()) + ".jpg");
Uri fromURI=Uri.fromFile(myImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fromURI);
startActivityForResult(intent,PulseConstants.CAMERA_REQUEST_CODE);
The image is stored correctly with the given file name in "My Files" folder of the phone...But when I open Gallery of the phone, this image does not appear ??
Please let me know if I am doing anything wrong??
TIA,
VijayRaj
This may have to do with the fact that the Android media scanner is not constantly indexing files on the phone. In the past I have called the following function immediately after saving an image to notify the MediaScanner:
private void scanMedia(File file) {
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);
}
Hope this works!
Related
I tried below code but when,it launches Google Drive App and shows me error not valid format
String completePath = Environment.getExternalStorageDirectory() + "/images/"+"rose.jpeg";
File file = new File(completePath);
Uri imageUri = Uri.fromFile(file);
Intent i = new Intent(Intent.ACTION_VIEW, imageUri);
startActivity(i);
I'm trying to open a file using another app, i.e. opening a .jpg with Gallery, .pdf with Acrobat, etc.
The problem I'm having with this is when I try to open the file in the app, it only opens the chosen app instead of opening the file within the app. I tried following Android open pdf file via Intent but I must be missing something.
public String get_mime_type(String url) {
String ext = MimeTypeMap.getFileExtensionFromUrl(url);
String mime = null;
if (ext != null) {
mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
}
return mime;
}
public void open_file(String filename) {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), filename);
// Get URI and MIME type of file
Uri uri = Uri.fromFile(file).normalizeScheme();
String mime = get_mime_type(uri.toString());
// Open file with user selected app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setType(mime);
context.startActivity(Intent.createChooser(intent, "Open file with"));
}
As far as I can tell, it returns the right URI and MIME type:
URI: file:///storage/emulated/0/Download/Katamari-ringtone-985279.mp3
MIME: audio/mpeg
Posting my changes here in case it can help someone else. I ended up changing the download location to an internal folder and adding a content provider.
public void open_file(String filename) {
File path = new File(getFilesDir(), "dl");
File file = new File(path, filename);
// Get URI and MIME type of file
Uri uri = FileProvider.getUriForFile(this, App.PACKAGE_NAME + ".fileprovider", file);
String mime = getContentResolver().getType(uri);
// Open file with user selected app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mime);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
I used this piece of code and it worked perfectly fine on android 6 and below not tested on the higher version
public void openFile(final String fileName){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory()
.getAbsolutePath()+ File.separator+"/Folder/"+fileName));
intent.setDataAndType(uri, "audio/mpeg");
startActivity(intent);
}
Want to pick image files from a specific folder vaibhav but have all the files of gallery instead.
Code
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Vaibhav/");
Log.e("Dir path",dir.toString());
dir.exists();
Uri uri = Uri.parse(dir.toString());
Intent browseIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.getContentUri(uri.toString()));
browseIntent.setType("image/jpg");
startActivityForResult(browseIntent, BROWSE_REQUEST);
Following Code Just Works Fine:
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Vaibhav/");
intent.setDataAndType(uri, "image/jpg");
startActivity(Intent.createChooser(intent, "Open folder"));
}
Use
browseIntent.setDataAndType(uri, "image/.");//Loads all image files
Instead of
browseIntent.setType("image/jpg");
Is it possible to Capture images via camera in android by passing uri of internal storage in the Intent? If yes How? I tried in the following way but it gave JHEAD can't read error.
File mediaStorageDir = getFilesDir();
File newFile = new File(mediaStorageDir,IMAGE_DIRECTORY_NAME);
Uri newUri = Uri.fromFile(newFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
It is because of small mistake. You are passing directory rather file name.
Try this way hope this helps you
File mediaStorageDir = getFilesDir();
File newFile = new File(mediaStorageDir,new Date().getTime() + ".png");
Uri newUri = Uri.fromFile(newFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
I am having a very difficult time with the camera intent and trying to get data about the photo that was just taken
I launch the camera like this
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir, "IMG_" + timeStamp + ".jpg");
photoUri = Uri.fromFile(output);
startActivityForResult(camera, CAMERA_REQUEST);
in my onActivityResult the intent is null so I cannot use anything there to get the Uri but doing this.
Bitmap photo = MediaStore.Images.Media.getBitmap(getContentResolver(),photoUri);
gets the photo just fine. What I want to do is get information about the photo Latitude,Longitude,time taken etc so I tried this
MediaStore.Images.Media.query(getContentResolver(),photoUri,null,null,null,null);
but the cursor is always returned as null so it obviously cannot find the uri. It seems that the image is not getting out into the MediaStore provider. How can I get the data from the photo just taken?