unable to store image in specific folder in sd card - android

I am capturing image by camera app and want to store in Specific folder but image not storing in folder here is my code :
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/" + "my_data");
try {
if (!dir.exists())
if (dir.mkdir()) {
System.out.println("Directory created");
Toast.makeText(Add.this, "created dir",
Toast.LENGTH_LONG).show();
}
image_path = Environment.getExternalStorageDirectory().getPath() + "/my_data/" + aa + "_image.jpg";
File file = new File(dir,image_path);
uriImage = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

EDIT
This should work, if not please post logcat output.
String folderPath = Environment.getExternalStorageDirectory().toString()
+ java.io.File.separator + "my_data"
);
File dir = new File(folderPath);
if(!dir.exists){
dir.mkdirs();
}
imagePath = folderPath+java.io.File.separator+aa+"_image.jpg";

Related

How to donwload image to gallery after FACE DETECTED in Android?

I can detect faces by the help of Google's sample vision app FaceTracker. What I want to do is to take photo of detected face and download it to phone Gallery.
File imagesFolder = new File( Environment.getExternalStorageDirectory() , "FolderName" );
imagesFolder.mkdirs();
final File image = new File( imagesFolder , "/" + "IMG_" +System.currentTimeMillis() + ".png" );
File file = new File( Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
imgCap.takePicture(image, new ImageCapture.OnImageSavedListener() {
#Override
public void onImageSaved(#NonNull File file) {
String msg = "Pic captured at " + image.getAbsolutePath();
//storing the image in a folder in gallery
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE );
Uri contentUri = Uri.fromFile( image );
mediaScanIntent.setData( contentUri );
getActivity().sendBroadcast( mediaScanIntent );

How to create folder in gallery and save file

Now I copy mp4 file from external storage folder and save copy file in gallery with folder. But My gallery app doesn't have folder I code. Surely, File too. But In File Browser, There are exists correctly in DCIM folder.
So, How can I save file in gallery with folder that I code. Please let me know If you can solve this issue.
private void saveToGallery(String recVideoPath) {
progressdialog = new CNetProgressdialog(this);
progressdialog.show();
String folderName = "DuetStar";
String fromPath = recVideoPath;
String toPath = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM;
File toPathDir = new File(toPath + "/" + folderName);
final File fromPathFile = new File(fromPath);
File toPathFile = new File(toPath + "/" + folderName, recVideoPath.substring(recVideoPath.lastIndexOf("/") + 1, recVideoPath.length()));
Log.d(TAG, "saveToGallery: " + RecordActivity.currentCreateFileName);
Log.d(TAG, "saveToGallery: " + toPathDir.toString());
Log.d(TAG, "saveToGallery: " + fromPath.toString());
Log.d(TAG, "saveToGallery: " + toPath.toString());
if (!toPathDir.exists()) {
toPathDir.mkdir();
} else {
}
FileInputStream fis = null;
FileOutputStream fos = null;
try {
if (toPathDir.exists()) {
fis = new FileInputStream(fromPathFile);
fos = new FileOutputStream(toPathFile);
byte[] byteArray = new byte[1024];
int readInt = 0;
while ((readInt = fis.read(byteArray)) > 0) {
fos.write(byteArray, 0, readInt);
}
Log.d(TAG, "saveToGallery: " + readInt);
fis.close();
fos.close();
Log.d(TAG, "saveToGallery: " + "Seucceful");
} else {
Toast.makeText(this, "There is no directory", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.getMessage();
}
progressdialog.dismiss();
}
You can save in specific folder as you wish see this code snippet for idea
String extStorageDirectory;
extStorageDirectory = Environment.getExternalStorageDirectory().toString() + "/Video Folder name/";
//making the folder
new File(extStorageDirectory).mkdirs();

Video not shown on Gallery

I have saved the video on SD card but its not showing in gallery
I can see the video in the sd card and its playing
I am using this method to get video uri and save it
public static Uri getVideoUri(Context context) {
Marapreferences marapreferences=Marapreferences.getInstance(context);
boolean ismedia=marapreferences.isMedia();
File file = null;
File file2 = new File(Environment.getExternalStorageDirectory()
+ "/mara_messenger/videos");
if (!file2.exists()) {
file2.mkdirs();
}
currentFileName = "" + System.currentTimeMillis() + ".mp4";
imageName = Environment.getExternalStorageDirectory()
+ "/mara_messenger/videos/" + currentFileName;
file = new File(imageName);
Uri imgUri = Uri.fromFile(file);
System.out.println("Image uri" + imgUri);
return imgUri;
}
File file=new File(uri);
MediaScannerConnection.scanFile(this, new String[] {file.getAbsolutePath()},
new String[]{"video/mp4"}, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri)
{
System.out.println("completed");
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(uri);
sendBroadcast(intent);
}
});
I can see after scan URI and path but still not showing on gallery

Creating folder with user input : Android

jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//int imageNum = 0;
String Name = PatientInfo.getText().toString();
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory().toString() + Name);
imagesFolder.mkdirs();
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
File output = new File(imagesFolder, s.toString() + ".jpg");
// String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
// File output = new File(imagesFolder, fileName);
/* while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}*/
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image saved: ",
Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{}
Log.d("Log", "onPictureTaken - jpeg");
}
};
This is a code example. I am trying to create folder with a userinput when it is entered. Each time i need to create new folders on entering new username. But in this there is something missing.Any suggestions will be helpful.
Change
File imagesFolder = new File(Environment.getExternalStorageDirectory().toString() + Name);
withFile
File imagesFolder = new File(Environment.getExternalStorageDirectory(), Name);
Please be sure to have also the WRITE_EXTERNAL_PERMISSION in the manifest, and that Name is not null

unfortunately camera has stopped working

I am new to android. I am capturing video using following code:
final int REQUEST_VIDEO_CAPTURED = 1;
Long tsLong = System.currentTimeMillis() / 1000;
String ts = tsLong.toString();
String imagepath = Environment.getExternalStorageDirectory() + "/"
+ galleryStart + "/" + FolderName + "/" + ts + ".mp4";
File file = new File(imagepath);
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);
If I haven't set path its work fine other wise it gives me error." unfortunately camera has stopped working." I am setting path for saving the videos in particular directory.
Camera App Doesn't make you dir which you asked for in your Uri. So try to make it first.
String imagepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + galleryStart + "/" + FolderName + "/" + ts + ".mp4";
File file = new File(imagepath);
try
{
if(file.exists() == false)
{
file.getParentFile().mkdirs();
file.createNewFile();
}
}
catch (IOException e)
{
Log.e(TAG, "Could not create file.", e);
}
Uri outputFileUri = Uri.fromFile(file);
Hope it helps you out!!

Categories

Resources