Dropbox API unable to upload onto user personal Dropbox? - java

I've succeeded onto transferring files onto my Dropbox account.
But when i share my app for testing with other members of the project, they can't transfer the files on their own Dropbox account.
And when they test the functionality, it transfers automatically onto my account.
Is there something I'm missing out here?
I know that i don't need to add manually users, they can automatically transfer the files they want.
Here's a snip of the method i used :
private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) {
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final String uploadPathF = uploadPathFrom;
final String uploadPathT = uploadPathTo;
Thread th = new Thread(new Runnable() {
public void run() {
File tmpFile = null;
try {
tmpFile = new File(uploadPathF);
} catch (Exception e) {
e.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(tmpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}

Related

Tesseract OCR not working for java web application

I am trying to develop Java web Tesseract OCR application. Following code works perfectly :
public class App {
public String getImgText(String imageLocation) {
ITesseract instance = new Tesseract();
instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"tessdata\").getPath() : "+Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
instance.setLanguage("eng");
try {
String imgText = instance.doOCR(new File(imageLocation));
return imgText;
} catch (TesseractException e) {
e.getMessage();
return "Error while reading image";
}
}
public static void main(String[] args) {
App app = new App();
System.out.println(app.getImgText("/home/user/Desktop/1.png"));
}
}
But when I trying to use the above code in my Java web(JSF) application after the line
ITesseract instance = new Tesseract();
nothing is printed out. Below is the code of my web application :
public String uploadImage(FileUploadEvent event) {
System.out.println("webcore bean");
//get uploaded file from the event
UploadedFile uploadedFile = (UploadedFile) event.getFile();
//create an InputStream from the uploaded file
InputStream inputStr = null;
try {
inputStr = uploadedFile.getInputstream();
} catch (IOException e) {
//log error
}
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
String directory = externalContext.getInitParameter("uploadDirectory");
String filename = FilenameUtils.getName(uploadedFile.getFileName());
File destFile = new File(directory, "static" + getFileExtension(filename));
//use org.apache.commons.io.FileUtils to copy the File
try {
FileUtils.copyInputStreamToFile(inputStr, destFile);
} catch (IOException e) {
//log error
}
System.out.println("getImageText(directory) : " + getImageText(directory));
FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
return null;
}
private String getImageText(String imageLocation) {
try {
System.out.println("Before ");
ITesseract instance = new Tesseract1();
System.out.println("After ");
//instance.setDatapath("/usr/share/tesseract-ocr/4.00/tessdata");
instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
instance.setLanguage("eng");
try {
String imgText = instance.doOCR(new File(imageLocation));
return imgText;
} catch (TesseractException e) {
e.getMessage();
return "Error while reading image";
}
} catch (Exception e) {
System.out.println("Before returning null");
e.printStackTrace();
return null;
}
}
The log "Before" is being printed but the log "After" is not being printed. I am using following technologies :
a) Ubuntu 18.04 64 bit OS
b) Netbeans
c) Maven
d) Glassfish 4.1

Problems in saving data with Android on CloudBoost

I'm using CloudBoost for Android application and i had some problems when I try to save save a data in a table, I'm not getting any results. This is my code:
CloudApp.init("*****", "*****");
...
new Thread(new Runnable() {
#Override
public void run() {
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1"); //Already this is not shown
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override
public void done(final CloudObject x, final CloudException e) {
if(e != null)
//error
Log.e("LOG", "Errore");
if(x!=null)
//cloudObject
Log.e("LOG", "FATTO");
}
});
} catch (CloudException e) {
e.printStackTrace();
}
}
});
Your log isn't shown because you made a Thread without calling start().
Android typically uses Asynctask anyway, but I'm not sure why you really need a Thread with this library... That save method looks asynchronous
CloudApp.init("*****", "*****")
CloudObject obj = new CloudObject("User", "1uvSDThQ");
Log.e("LOG", "1");
try {
obj.set("color", "#000000");
obj.setAcl(new ACL());
obj.save(new CloudObjectCallback() {
#Override

manage many documents scan with a java application

I am trying to make an application to scan documents from a scanner and i have found an application based on the mmscomputing free library that i found in github https://github.com/ashishkataria/browserWebScanning
It shows a panel from where you can choose a scanner from a list of available ones and scan the document.
public void getScan()
{
try
{
scanner.acquire();
}
catch (ScannerIOException e1)
{
IJ.showMessage("Access denied! \nTwain dialog maybe already opened!");
e1.printStackTrace();
}
}
public Image getImage()
{
Image image = imp.getImage();
return image;
}
public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata) {
if (type.equals(ScannerIOMetadata.ACQUIRED))
{
if(imp!=null)
{
jContentPane.remove(ipanel);
jContentPane.remove(cpanel);
jContentPane.remove(crpdpanel);
}
imp = new ImagePlus("Scan", metadata.getImage());
im = imp.getImage();
imagePanel = new ImagePanel(im);
imagePanel.updateUI();
imagePanel.repaint();
imagePanel.revalidate();
ClipMover mover = new ClipMover(imagePanel);
imagePanel.addMouseListener(mover);
imagePanel.addMouseMotionListener(mover);
ipanel = imagePanel.getPanel();
ipanel.setBorder(new LineBorder(Color.blue,1));
ipanel.setBorder(BorderFactory.createTitledBorder("Scanned Image"));
ipanel.setBounds(0, 30,600, 600);
ipanel.repaint();
ipanel.revalidate();
ipanel.updateUI();
jContentPane.add(ipanel);
jContentPane.getRootPane().revalidate();
jContentPane.updateUI();
cpanel = imagePanel.getUIPanel();
cpanel.setBounds(700, 30,300, 150);
cpanel.repaint();
cpanel.setBorder(new LineBorder(Color.blue,1));
cpanel.setBorder(BorderFactory.createTitledBorder("Cropping Image"));
cpanel.setBackground(Color.white);
jContentPane.add(cpanel);
jContentPane.repaint();
jContentPane.revalidate();
metadata.setImage(null);
try {
new uk.co.mmscomputing.concurrent.Semaphore(0, true).tryAcquire(2000, null);
} catch (InterruptedException e) {
IJ.error(e.getMessage());
}
}
else if (type.equals(ScannerIOMetadata.NEGOTIATE)) {
ScannerDevice device = metadata.getDevice();
try {
device.setResolution(100);
} catch (ScannerIOException e) {
IJ.error(e.getMessage());
}
try{
device.setShowUserInterface(true);
device.setResolution(100); }catch(Exception e){
e.printStackTrace(); }
}
else if (type.equals(ScannerIOMetadata.STATECHANGE)) {
System.out.println("Scanner State "+metadata.getStateStr());
System.out.println("Scanner State "+metadata.getState());
if ((metadata.getLastState() == 3) && (metadata.getState() == 4)){}
} else if (type.equals(ScannerIOMetadata.EXCEPTION)) {
IJ.error(metadata.getException().toString());
}
}
I am trying to make it handle multiple documents and save in a pdf file, this library handles only 1 document right now.
i want to know how can i save the images in a buffer or something else and rescan until the user is done .
and is there a function that can save those images in a pdf file ?

Android - Google Drive API file never syncs

I have a weird bug I'm hoping someone can help me with.
I am using the Google Drive app folder to backup some data from my app. It creates the file and writes my data without and issue, I can sign grab the file from drive at any time and it works fine.
However if I uninstall the app when I reinstall it the Google Drive App folder is empty. I'm assuming I'm missing some point of the sync process so it's only caching locally but I can't see what I've done wrong. My code to create and commit the file is below.
DriveFile file = ..//Get drive file;
file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
#Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Problem opening file, status is " + result.getStatus().toString());
return;
}
Log.i(TAG, "File opened");
writeFileContents(result.getDriveContents());
}
});
private void writeFileContents(final DriveContents contents) {
new AsyncTask<Object, Object, Integer>() {
#Override
protected Integer doInBackground(Object[] params) {
try {
ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
// Overwrite the file.
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
.getFileDescriptor());
Writer writer = new OutputStreamWriter(fileOutputStream);
SyncUtils.writeXml("some xml", writer);
writer.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
#Override
protected void onPostExecute(final Integer x) {
contents.commit(mGoogleApiClient, null).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
#Override
public void onResult(com.google.android.gms.common.api.Status result) {
if(result.getStatus().isSuccess())
Log.i(TAG, "Write succeeded");
else
Log.i(TAG, "Write failed");
onConnectionCallbacks.onSynced(x);
}
});
}
}.execute();
According to the documentation, contents.getParcelFileDescriptor() only works with DriveFile.MODE_READ_WRITE. You are using DriveFile.MODE_WRITE_ONLY, so you should be calling contents.getOutputStream().

Renaming a file on HDFS works in local mode but not in cluster mode

I have an object in charge of opening a file on HDFS to write. This object renames the file it just wrote once the close() method is invoked.
The mechanism works when running in local mode, but it fails to rename the file in cluster mode.
//Constructor
public WriteStream() {
path = String.format("in_progress/file");
try {
OutputStream outputStream = fileSystem.create(new Path(hdfs_path+path), new Progressable() {public void progress() { System.out.print("."); }
});
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Did you experience that before ?
Apparently FileSystem.rename(Path) creates missing directories on the path when executed in local mode, but it does not when run in cluster mode.
This code works in both modes:
public void close() {
String dirPath = String.format("%s_dir/", date, timestamp);
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.mkdir(new Path(hdfs_path+dirPath));
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Just curious, but how can you rename a file that officially doesn't exist (because you're still writing at that point)?
The fix is to rename after the file has been completed. That is, when you invoked the close method.
So your code should look like this:
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
writer.close();
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
} catch (IOException e) {
e.printStackTrace();
}
}

Categories

Resources