Capture images directly in internal storage - android

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);

Related

Want to pick image files from a specific folder but have all the files of gallery instead

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");

play video from android internal storage

I'm trying to play video files stored in android internal memory through intent. Files exist there but when i try to play them through intent, it give me the error "Media file not supported" or "can't play video" depending on device. I couldn't find where i'm wrong. here is my code
File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, filename);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.parse(videoResource);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
I don't know where i'm wrong and what should i do. any help would be much appreciated. Thank you :)
Thank you so much friends for participation, your efforts are very much appreciated. I'v found the solution of my problem. There is need to set file read true before playing it.
fileWithinMyDir.setReadable(true, false);
Now here is complete code of intent to play mp4 video from internal storage of android.
File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, headingsList.get(mPosition));
fileWithinMyDir.setReadable(true, false);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.fromFile(new File(videoResource));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
This code works for me:
Uri fileUri = Uri.fromFile(new File(String file_path));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);

Image not appearing in Gallery

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!

How can I show file in sdcard to user?

I want to show user my apk file using intents, here is the code:
SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,fileName);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
startActivity(intent);
but it is giving me ActivityNotFoundException,I want to start the apk file so that user can install it.
Try this?
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
intent.setDataAndType(path, "application/vnd.android.package-archive");
startActivity(intent);

Android - Obtain Uri to image on sdcard

I have the absolute path to a .jpg in the gallery.
How can I obtain a 'content://' Uri to this image, so that I can use this Uri like this:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
I need to launch this intent so I can view the image.
How do I obtain the uri, when I have got only the absolute path to the image?
First you need to read the file from your SDCard.
See:
reading a specific file from sdcard in android
http://developer.android.com/reference/java/io/File.html
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
Then call the File.toUri() method to retreive the URI from the file.
Intent intent = new Intent(Intent.ACTION_VIEW, yourFile.toURI());
startActivity(intent);

Categories

Resources