Im trying to save the image taken by teh Camera intent. My code wasnt working so when i added the check whether the directory is even made, it should that it wasnt.What can possibly be wrong?
Im trying on the emulator.
01-11 13:50:28.946: D/file(1161): file is /mnt/sdcard/M3
01-11 13:50:28.946: D/file(1161): photo is /mnt/sdcard/M3/3_1.jpg
I get the above in the log.
Below is the code i have on the button which opens camera
File sdCard = Environment.getExternalStorageDirectory();
File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");
file.mkdirs();
String name = e_id + "_" + (size+1)+ ".jpg";
File newfile = new File(file,name);
newfile.mkdir();
Log.d("file"," file is " + file.toString());
Log.d("file"," photo is " + newfile.toString());
if(!file.mkdir())
{
Log.d("file"," not created ");
}
if(!newfile.mkdir())
{
Log.d("newfile"," not created ");
}
else
{
outputFileUri = Uri.fromFile(newfile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
The issue is that you are treating the image file as a directory:
newfile.mkdir();
Try:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath(), File.separator + "/M3");
if (!dir.mkdirs())
{
Log.e(TAG, "Failed to create directory " + dir);
return false; // Assuming this is in a method returning boolean
}
String filename = e_id + "_" + (size+1)+ ".jpg";
File file = new File(dir, filename);
Log.d(TAG, "dir is " + dir.toString());
Log.d(TAF, "file is " + file.toString());
outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
Note: you need to name your variables better; file isn't a file it's a directory. newfile is the only file you care about in this code snippet, so call it file.
replace this line
File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");
with this one.
File file = new File (sdCard.getAbsolutePath()+"/YourDirectoryName");
Related
I know there are many other posts with this issue, but none seem to solve mine. I am trying to create a file on Enviroment.DIRECTORY_DOWNLOADS, email that file, and then delete it. However, I am getting an error when I initialize the file output stream fos = new FileOutputStream(file);.
The error I get is:
java.io.FileNotFoundException: /CheckListReport_2015_07_19.txt: open failed: ENOENT (No such file or directory)
In my manifest I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> outside of the applications tag
Below is the code:
String report = new SimpleDateFormat("yyyy_MM_dd").format(Calendar.getInstance().getTime());
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
String lineToWrite;
for (Map.Entry entry : CheckboxHandler.data.entrySet()) {
lineToWrite = entry.getKey() + ", " + entry.getValue() + "\n";
fos.write(lineToWrite.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
String subject = "CheckListReport_" + report + ".txt";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"checklistreportdata#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
file.delete();
use this permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Try this code to read files from Download directory:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
Change this:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + Environment.DIRECTORY_DOWNLOADS + "/CheckListReport_" + report + ".txt"));
to:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
I have some code like this:
photo = new File(android.os.Environment.getExternalStorageDirectory(), "images/mydir/" + File.separator + timeStamp + ".jpg");
photo.getParentFile().mkdirs();
photo.createNewFile();
But in the gallery, I'm only seeing "mydir" with the photos inside it. No "images" directory to be found.
How do I create a hierarchy so there's an image folder and mydir is inside that?
Thanks
You have problem in your code as you have one / after mydir and you are also adding File.separator so final output will be images/mydir//
photo = new File(android.os.Environment.getExternalStorageDirectory(), "images/mydir/" + File.separator + timeStamp + ".jpg");
Just remove the / after mydir like below
photo = new File(android.os.Environment.getExternalStorageDirectory(), "/images/mydir" + File.separator + timeStamp + ".jpg");
This should work now.
I am creating application folder on first activity on onCreate() Method but folder is not creating.here is the code
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File fil = new File(this.getFilesDir().getPath()+File.separator+"MyContactsBackUp");
fil.mkdirs();
} else {
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"MyContactsBackUp");
directory.mkdirs();
}
Try this code
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File podcastFolder = new File(Environment.getExternalStorageDirectory() + File.separator
+ getString(R.string.app_name));
} else {
/* save the folder in internal memory of phone */
File podcastFolder = new File("/data/data/" + getPackageName()
+ File.separator + getString(R.string.app_name));
}
Try this.
File f = new File(android.os.Environment.getExternalStorageDirectory(),File.separator+"MyContactsBackUp/");
f.mkdirs();
or for internal memory of application instead of write hardcore sting use.
File f = new File(getCacheDir(),File.separator+"MyContactsBackUp/");
f.mkdirs();
I used a tutorial to download a zip into a subdirectory of my application's internal storage. I wrote the zip to /data/data/my.package.name/files/mySubDirectory/the.zip.
But, when I check to see whether the zip exists, it doesn't:
String fileDirectory = this.getFilesDir().getAbsolutePath() + "/mySubDirectory/the.zip";
File file = new File(fileDirectory);
if(file.exists()) {
Log.e(this.class.getName(), "file exists");
} else {
Log.e(this.class.getName(), "file doesn't exist");
}
I verified that fileDirectory is the same path as the File outFile for the FileOutputStream.
What could be the problem?
Try getting your file path as below :
String fileDirectory=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "data" + File.separator+ getActivity().getPackageName()+ File.separator +"mySubDirectory"+File.separator+"the.zip";
Using this SO question, I created a subdirectory using this example:
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file
The problem is that I didn't expect the subdirectory to be prepended with "app_", so I was looking for the zip in the wrong place.
Try using getFilesDir() + "/" subdirectory + "/" "the.zip"
Without the getabsolutepath().
That is what I used could be the issue.
OK maybe you problem is with permissions do you see the file in the DDMS under data/data/package/files ? Check the permissions for the files
Here is my code
String path = getFilesDir() + "/"
+ subDirName + "/";
File file = new File(path);
file.mkdirs();
setReadable(file);
I use the following to make the file readable
#TargetApi(9)
private void setReadable(File file) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
try {
Runtime.getRuntime().exec(
"chmod 777 " + file.getCanonicalPath());
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
file.setReadable(true, false);
}
}
}
My problem is whenever I save the bitmap. I saves one in the folder I want(MyFolder) and one in the DCIM/Camera. I don't see any code that makes it save int he DCIM directory?
Here are my codes...
case R.id.menu_save:
try {
String path = Environment.getExternalStorageDirectory()
.toString();
File myNewFolder = new File(path + "/MyFolder");
myNewFolder.mkdirs();
OutputStream fOut = null;
File file = new File(path, "/MyFolder/HK" + filename + ".jpg");
fOut = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
Toast.makeText(getApplicationContext(),
filename + "Has been saved!", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(
getApplicationContext(),
"Problem to Save the File", Toast.LENGTH_LONG).show();
}
break;
As in the thread #Dixit linked, you can specify the file path URI with
File fileDir = new File(Environment.getExternalStorageDirectory() +
"/saved_images");
fileDir.mkdirs();
File file = new File(fileDir, "image.jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
This way the camera will save the image to the specified path instead of the DCIM folder.
EDIT: You have to create the folder on the sdcard beforehand, maybe that's the problem. Otherwise, this should work.