I want to save my file directly into the server and not into my device. currently, the converted PDF file is being saved into my phone's storage. how do i get it to send that file into the server directly?
the path that i want to save the file to is:
"http://"+IP+"/OMAS/docs/"
my code that contains the path that saves into the internal storage:
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/");
path = path + filename + ".pdf";
Document document = new Document(PageSize.A4, 38, 38, 50, 38);
Rectangle documentRect = document.getPageSize();
try {
PdfWriter.getInstance(document,new FileOutputStream(path));
Related
I am trying to create a folder on my external SDcard that will contain a text file that I can pull off my phone from my PC and read it.
I have pieced together a snippet that I believe should work, but I cannot find the folder that is supposedly being created.
File path = Environment.getExternalStorageDirectory();
File dir = new File(path.getAbsolutePath() + "/iptestdata/");
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = new SimpleDateFormat("yyyyMMddHHmmss'.txt'").format(new Date());
final File file = new File(dir, fileName);
As I am stepping though the debugger in Android Studio, I see the path being created and stored in dir is /storage/emulated/0/iptestdata, and I see the file name appearing in the format that I requested, but I cannot find the file via File Explorer in Windows. How can I fix this?
If you create File and store into storage Directory you want to call mkdirs()
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + " /dir - name /");
dir.mkdir();
File file = new File(dir, "File-Name.txt");
FileOutputStream os = outStream = new FileOutputStream(file);
String data = "This is the content of my file";
os.write(data.getBytes());
os.close();
Please check the Directory is created or not the following method
public static boolean canWriteOnExternalStorage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.v("Activity", "Yes, can write to external storage.");
return true;
}
return false;
}
Add Permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I'm able to save a Image in disk using :
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = imageId + ".png";
String filePath = baseDir + File.separator + fileName;
File file = new File(filePath);
if(!file.exists())
{
out = new FileOutputStream(baseDir + File.separator + fileName);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
But all the images saved are visible in the gallery of the phone .. How can I hide them to the user ? Is there a private folder for my appliaction ?
Thanks
Save it to the internal storage using Context.getFilesDir() instead of Environment.getExternalStorageDirectory().
From the docs:
Files saved here are accessible by only your app by default.
Check this link out.
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
I think this is what you want. Read https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage for more information.
I need to save file into internal storage, an then read it.
For save it i do something like this:
// Create directory into internal memory;
File mydir = getActivity().getDir("modelli", Context.MODE_PRIVATE);
File a = new File(mydir, obj_id);
a.mkdir();
File b = new File(mydir, obj_id);
if (obj_type != "modello"){
b = new File(a,obj_type);
b.mkdir();
}
String filename = "";
if(obj_imageName != null)
{
filename = obj_imageName + "." + getFileExtension(urlString);
}
else
{
filename = getFileName(urlString);
}
final File file = new File(b, filename);
file.createNewFile();
but I don't know how read it, with this I get an error (contains a path separator):
File mydir = getActivity().getDir("app_modelli/2/images", Context.MODE_PRIVATE);
where is the mistake? is correct my approach for write file?
You better put them in internal files dir. Both for writing and reading you could use a File object like:
String fileName = "info.txt";
File file = new File ( getInternalFilesDir() + "/" + fileName);
At the moment your code only creates a file with length 0. You are not writing/saving anything to it.
I have one requirement in my Android application. I need to download and save file in specific folder of SD card programmatically. I have developed source code, which is
String DownloadUrl = "http://myexample.com/android/";
String fileName = "myclock_db.db";
DownloadDatabase(DownloadUrl,fileName);
// and the method is
public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/myclock/databases");
if(dir.exists() == false){
dir.mkdirs();
}
URL url = new URL("http://myexample.com/android/");
File file = new File(dir,fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager" , "download url:" +url);
Log.d("DownloadManager" , "download file name:" + fileName);
URLConnection uconn = url.openConnection();
uconn.setReadTimeout(TIMEOUT_CONNECTION);
uconn.setConnectTimeout(TIMEOUT_SOCKET);
InputStream is = uconn.getInputStream();
BufferedInputStream bufferinstream = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while((current = bufferinstream.read()) != -1){
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream( file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");
int dotindex = fileName.lastIndexOf('.');
if(dotindex>=0){
fileName = fileName.substring(0,dotindex);
}
catch(IOException e) {
Log.d("DownloadManager" , "Error:" + e);
}
}
Now the issue is only empty file with filename myclock_db.db is saving in the path. but I need to download and save content of file in the specific folder. Tried several ways to get the file download, but I can't.
Your download URL is not a link to any file. It's a directory. Make sure its a file and exists. Also check your logcat window for error logs. One more suggestion, its always better to do a printStackTrace() in catch blocks instead of Logs. Its gives a more detailed view of the error.
Change this line:
URL url = new URL("http://myexample.com/android/");
to:
URL url = new URL("http://myexample.com/android/yourfilename.txt"); //some file url
Next, in catch block, add this line:
e.printStackTrace();
Also in the directory path, it should be something like this:
File dir = new File(root.getAbsolutePath() + "/mnt/sdcard/myclock/databases");
instead of
File dir = new File(root.getAbsolutePath() + "/myclock/databases");
Next, make sure you have acquired permission for writing to external storage in Android manifest.
I am trying to record voice and save it in a file. For this I have to give a path to save the file, but I don't know how will I set the path...I have recently started working on android phone. In windows we set path like a drive e.g. C:/folder/folder....in android phone what will be my root? Where can I save an audio file? Write now I am working on emulator...Will the path be same for both emulator and phone?
we can create file like this in SD card
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/folder");
myDir.mkdirs();
String fname = "file";
File file = new File (myDir, fname);
file is the path of file where you stored.
and add this Permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
_path=Environment.getExternalStorageDirectory().getPath() + "/RECORDS/";
// || ||
// VV VV
// storage path folder name
fileName = String.format("filename.mp3");
File file = new File(_path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Save your file in Sdcard.
File path = Environment.getExternalStorageDirectory();
String cardName = path.getName();
path=cardName+"/mypathname/file_name.mp3";
It is also possible in emulator. create sdcard , when you create emulator.
Thanks
Uri outputFileUri;
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
sdImageMainDirectory = new Filenter code here(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);