i am using this following code to save a created pdf inside my phone;
File file = new File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),"/" +user.getInjurdManFirstName() +user.getInjuredManSecondName()+ "-" + user.getIdNumber()+".pdf");
try{
myPdfdocument.writeTo(new FileOutputStream(file));
showMessage("SUCCES!");
}catch (IOException e){
e.printStackTrace();
showMessage("NO SUCCES");
}
the code works good but it save me the pdf file in the download folder, i want it to save it like: DOWNLOADS/"custom folder/"name.pdf"
i tried to use Context and its doesnt work
Related
In my Android App, in one of the activity, there is a "PDF Download" button. When this button is clicked, I create a PDF and then download it to the "Downloads" directory. Following is my code which runs on the click event of the button:
public void createAndDownloadPDF()
{
try
{
PdfDocument document = new PdfDocument();
View content = this.findViewById(android.R.id.content);
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(content.getWidth(),
content.getHeight() - 20, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
content.draw(page.getCanvas());
document.finishPage(page);
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyhhmmss");
String pdfName = "pdfdemo"
+ sdf.format(Calendar.getInstance().getTime()) + ".pdf";
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), pdfName);
try
{
outputFile.createNewFile();
OutputStream out = new FileOutputStream(outputFile);
document.writeTo(out);
document.close();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Please note that I added the required WRITE permission to the MANIFEST file and also asking for the permission at runtime. So, there is no issue of permissions.
When I debug the code, I notice that "outputFile" variable holds this path:
/storage/emulated/o/Download/pdfdemo07052017121233.pdf
My users will never find above path in their mobile. So, they will have no clue where their PDF got saved. I want that when users click on "Downloads" icon on their mobile, they should see the PDF file they downloaded from the app.
So, I think if I can sort out the following line:
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), pdfName);
my work will be done. What should be the path I use so that my PDF files get saved / downloaded in the "Downloads" directory? I researched over the internet, but did not find any concrete solution.
Try this:
File outDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
If this doesn't work, evaluate the chance of having your PDF file previously generated and saved into a folder inside your Assets folder. If this can be the case, after the above line you insert this:
copyAssets("XXX",outDir.toString());
Where "XXX" is the name of the folder inside your Assets folder which will contain the PDF file. This will copy the contents of XXX to DOWNLOADS/XXX on your device.
I am trying to pull multiple images from gallery and place in grid view , Unfortunately I am not able to do so, Can you help me with that. And also I made a folder in sd card and I m trying to store audio as well as photographs in the same folder. Can you help me with that as well?
I am using android 2.3.
So I have included this code inside the button click but on click of button it has created the folder but its not storing the file inside the folder. On click of the button every other thing is working, its opening the gallery and its calculating numpic. I am not sure why its not storing the file in CN Video folder.
public void onClick(View v) {
Intent pass_data = new Intent(MainRecord.this, OpenGallery.class);
pass_data.putExtra("numpic",numpic);
startActivity(pass_data);
// Saving Audio recorded file to directory
File f = new File(Environment.getExternalStorageDirectory() + "/CNvideo");
if(f.isDirectory()) {
//Write code for the folder exist condition
}else {
// create a File object for the parent directory
File CNvideoDirectory = new File("/sdcard/CNVideo/");
// have the object build the directory structure, if needed.
CNvideoDirectory.mkdirs();
// create a File object for the output file
String filename = getfilename();
File outputFile = new File(CNvideoDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
try {
FileOutputStream fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}};
public void write(View view)
{
String state;
File Dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (!Dir.exists())
{
Dir.mkdir();
}
File file = new File(Dir,"nahk.txt");
Toast.makeText(getApplicationContext(),file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
String Message = "5nahk";
try {
FileOutputStream FOS = new FileOutputStream(file);
FOS.write(Message.getBytes());
FOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I have added write permission in the manifest xml file. This method is called when I press a button. The toast says that the txt file is saved in the Download folder of my Internal Storage (since I don't have an SD card in my LG G3). I open the file location (using FileManager on my LG G3) and there is no "nahk.txt" in that folder. Why can't I see the file?
I'm not familiar with native android development but I encountered something similar using cordova. After saving a file the file was not visible browsing with a usb cable but it was actually there. To see it had to restart my device.
So you could try to restart your device and check if it is still invisible (or create a method which fetches or checks the existence of the saved file to check if it is there).
edit: I learned this by googling so you should find it also
Try this
File Dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Your_Folder_Name";
File file = new File(Dir,"nahk.txt");
this file will save inside of your device storage Your_Folder_Name
I am writing an app to create a folder on my Nexus 5 and then write a text file inside the folder.
The above part is working just fine. I am writting using the following code:
Creating the folder:
File sdCard1 = Environment.getExternalStorageDirectory();
File dir = new File(sdCard1.getAbsolutePath() + "/SmsApp");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
File sdCard = Environment.getExternalStorageDirectory();
directory = new File (sdCard.getAbsolutePath() + "/SmsApp");
directory.mkdirs();
And writing the string to text file.
public void writeToText(String texttosave)
{
File sdCard = Environment.getExternalStorageDirectory();
File logFile = new File(sdCard.getAbsolutePath() + "/SmsApp" + "/smsrawdata.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
try
{
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(texttosave);
buf.newLine();
buf.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
Text file is showing up properly on my phone but when I am trying to access the same folder on phone I can't see the folder at all.
I thought it would be the same for other app on play store that creates a folder but then everything gets created and then I can see them on Windows Explorer.
Am I doing something wrong in the code part for it not to show on my Computer but show only on my phone. I am using ES File Explorer to see files on my phone.
Please let me know if someone else is having the same problem.
Thanks!
Your pc will communicate with the media store about files. Not directly with the sdcard or external memory. If the media store does not know about your file your pc can not see it. You forgot to tell the store that you created a file. For every new file you should invoke the media scanner for that file. You are not the first one who happens this so the problem has been reported many times. You only need to add a few lines of code which you will find easily searching this site for invoking media scanner on new file. If you switch off/on your device the file will be known soon too.
I am using AsyncTask to download a file from a web folder. However, the name of the file will be different every time.
Is there a way to get the name of the file in a web folder? (There will be only 1 file in that folder at a given time).
http://www.example.com/myfolder/myfile_1
Try the ApacheURLLister (I didn't tested it but it may work):
URL url;
List serverDir = null;
try {
url = new URL("http://www.abc.com/myfolder/");
ApacheURLLister lister = new ApacheURLLister();
serverDir = lister.listAll(url);
}
catch (Exception e) {
e.printStackTrace();
Log.e("ERROR ON GETTING FILE","Error is " +e);
}
System.out.println(serverDir);
return serverDir;