File exist in assets but is throwing an FileNotFoundException - assets

I am trying to access photos saved in the assets folder, my code doen't work all the times.
i have 16 sub-folders in assets with photos in png format each have between 30kb ~ 60kb
Each folder have between 100 and 400 photos
Full size of assets folder is 113mb.
My code stoped working when i added more photos to the assets
public Bitmap getImg(String ID, String pic){
InputStream istr = null;
try {
//Exemple: II/1.png
istr = getAssets().open(ID + "/" + pic +".png");
} catch (IOException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream(istr);
}
When i run this code i get a FileNotFoundException, but the file existis (i am awere that file names are case sensitive, extension is in lowercase and pic names are numbers).
I think this is something to do with the file size, because i compressed all photos in one of the subfolders and it started to work again, but on the other folders (that have alot more photos) don't, even after compressing i keep getting a FileNotFoundException.
is there a better way to store or access the photos?

I was able to fix my issue by changing the format from PNG to JPG

Related

A question regarding pptx files in react-native app

I have an application where I need to show the .pptx and .pdf files. For pdf files I am using react-native-pdf and file is opening fine in my App but when it comes to .pptx files we have 2 libraries:
1. https://www.npmjs.com/package/react-native-doc-viewer
2. https://www.npmjs.com/package/react-native-file-viewer
react-native-doc-viewer is not being actively maintained and a lot of issues :(
But both of them were giving a prompt to select an app like Wps Office or Microsoft apps but they were not opening as Pdf files opened in my app. Whats the reason behind this? We cannot open pptx file in our app?
I read the react-native-doc-viewer android native code. it is actually is to download a doc not to view it. the following is the code:
#ReactMethod
public void openDoc(ReadableArray args, Callback callback) {
final ReadableMap arg_object = args.getMap(0);
try {
if (arg_object.getString("url") != null && arg_object.getString("fileName") != null) {
// parameter parsing
final String url = arg_object.getString("url");
final String fileName =arg_object.getString("fileName");
final String fileType =arg_object.getString("fileType");
final Boolean cache =arg_object.getBoolean("cache");
final byte[] bytesData = new byte[0];
// Begin the Download Task
new FileDownloaderAsyncTask(callback, url, cache, fileName, fileType, bytesData).execute();
}else{
callback.invoke(false);
}
} catch (Exception e) {
callback.invoke(e.getMessage());
}
}
it uses FileDownloaderAsyncTask to download files. if you are familiar with it.
if you want to show excels, Docx, you can use the google doc line convert it to Html, then in the webView to show it. the format like it: https://docs.google.com/gview?embedded=true&url=[doc address], the same effect as ios.

Process.Start PDF in a folder

I can't get Process.Start to simply launch a PDF with default PDF viewer.
I tried so many combinations of shell execute, working folder etc etc. Keeps giving me either 'The system cannot find the file specified' or 'the directory name is invalid'
private void button1_Click(object sender, EventArgs e)
{
string filename = #"Milking and cooling software set 2018-39.pdf";
MessageBox.Show(currentpath + #"\Astronaut A5 v1.5(b7)\documentation\" + filename);
fullpath = currentpath + #"\Astronaut A5 v1.5(b7)\documentation";
fullfile = fullpath + filename;
ProcessStartInfo process = new ProcessStartInfo();
process.WorkingDirectory = fullpath;
process.UseShellExecute = false;
process.FileName = fullfile;
process.RedirectStandardOutput = true;
process.Verb = "run as";
Process.Start(process);
}
Why is this so hard, I have tried for hours to simply lauch Acrobat Reader to open a PDF file. I can double click it no problem in it's location but C# can't open it, either I get .NET errors or Adobe opens and says it can't find the file. Tried so many combinations of "\"", full path, hard coded path etc etc...unbelievable that this is so hard to code in this day and age.
You’ve told the system to not use ShellExecute. This means the path you’re giving should be an actual executable program. PDFs are not so if you want to open it with the default reader use ShellExecute.
process.UseShellExecute = true;
Also using “run as” as the verb doesn’t make any sense here, unless there is such a verb defined for PDFs which I’m pretty sure there isn’t. That should be removed.

How to merge 10000 pdf into one using pdfbox in most effective way

PDFBox api is working fine for less number of files. But i need to merge 10000 pdf files into one, and when i pass 10000 files(about 5gb) it's taking 5gb ram and finally goes out of memory.
Is there some implementation for such requirement in PDFBox.
I tried to tune it for that i used AutoClosedInputStream which gets closed automatically after read, But output is still same.
I have a similar scenario here, but I need to merge only 1000 documents in a single one.
I tried to use PDFMergerUtility class, but I getting an OutOfMemoryError. So I did refactored my code to read the document, load the first page (my source documents have one page only), and then merge, instead of using PDFMergerUtility. And now works fine, with no more OutOfMemoryError.
public void merge(final List<Path> sources, final Path target) {
final int firstPage = 0;
try (PDDocument doc = new PDDocument()) {
for (final Path source : sources) {
try (final PDDocument sdoc = PDDocument.load(source.toFile(), setupTempFileOnly())) {
final PDPage spage = sdoc.getPage(firstPage);
doc.importPage(spage);
}
}
doc.save(target.toAbsolutePath().toString());
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}

Uploading PDF documents to a SQL Server

We have a scanner that is going to scan documents into a specific directory.
I need ideas on where to start on an application/service that I can schedule to run every hour or so and upload the documents to a Cloud DB.
Every single example I can find involves the uploader that requires a user to browse/select the file; I don't want that.
The documents will be stored as binary if that matters.
Maybe this will help you get started. Build a console app that does something along these lines
void Main()
{
while(true)
{
var files = Directory.GetFiles("c:\\your\\path\\here\\","*.pdf");
foreach (var file in files)
{
UploadToCloudDB(file);
//possibly delete the file now
}
Thread.Sleep(30000); //pause for 30 seconds between scans
}
}
static void UploadToCloudDB(string path)
{
var bytes = File.ReadAllBytes(path);
//upload those bytes to the cloud DB ....
}

context path for file upload without HttpRequest in REST application

I am building REST application. I want to upload a file and I want to save it for example in /WEB-INF/resource/uploads
How can I get path to this directory ? My Controller looks like this
#RequestMapping(value = "/admin/house/update", method = RequestMethod.POST)
public String updateHouse(House house, #RequestParam("file") MultipartFile file, Model model) {
try {
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (file.getSize() > 0) {
inputStream = file.getInputStream();
fileName = "D:/" + file.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
int readBytes = 0;
byte[] buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
}
} catch(Exception ex) {
ex.printStackTrace();
}
model.addAttribute("step", 3);
this.houseDao.update(house);
return "houseAdmin";
}
Second question...what is the best place to upload user files ?
/WEB-INF is a bad place to try to store file uploads. There's no guarantee that this is an actual directory on the disk, and even if it is, the appserver may forbid write access to it.
Where you should store your files depends on what you want to do with them, and what operating system you're running on. Just pick somewhere outside of the webapp itself, is my advice. Perhaps create a dedicated directory
Also, the process of transferring the MultipartFile to another location is much simpler than you're making it out to be:
#RequestMapping(value = "/admin/house/update", method = RequestMethod.POST)
public String updateHouse(House house, #RequestParam("file") MultipartFile srcFile, Model model) throws IOException {
File destFile = new File("/path/to/the/target/file");
srcFile.transferTo(destFile); // easy!
model.addAttribute("step", 3);
this.houseDao.update(house);
return "houseAdmin";
}
You shouldn't store files in /WEB-INF/resource/uploads. This directory is either inside your WAR (if packaged) or exploded somewhere inside servlet container. The first destination is read-only and the latter should not be used for user files.
There are usually two places considered when storing uploaded files:
Some dedicated folder. Make sure users cannot access this directory directly (e.g. anonymous FTP folder). Note that once your application runs on more than one machine you won't have access to this folder. So consider some form of network synchronization or a shared network drive.
Database. This is controversial since binary files tend to occupy a lot of space. But this approach is a bit simpler when distributing your application.