I want to send a file in a byte array through http post in a class called Add.
In a class called FileChooserActivity (that extends FragmentActivity), I have a method to select the file called onFileSelected():
@Override
public void onFileSelected(File file) {
if (file != null) {
if (file.isDirectory()) {
replaceFragment(file);
} else {
finishWithResult(file);
setFile(file);
FileUtils futils = new FileUtils();
try {
futils.fullyReadFileToBytes(file);
} catch (IOException e) {
e.printStackTrace();
}
Log.v("file", file.toString());
Toast.makeText(FileChooserActivity.this, "File uploaded", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(this,AddAlerts.class);
intent.putExtra("file", file);
startActivity(intent);
}
} else {
Toast.makeText(FileChooserActivity.this, "Error selecting File", Toast.LENGTH_SHORT).show();
}
}
public File setFile(File file){
return file;
}
In a class called FileUtils, I have a method to convert the file to a byte array called fullyReadFileToBytes:
public byte[] fullyReadFileToBytes(File file) throws IOException {
Log.v("fullyReadFileToBytes", file.toString());
int size = (int) file.length();
byte bytes[] = new byte[size];
byte tmpBuff[] = new byte[size];
FileInputStream fis= new FileInputStream(file);
try {
int read = fis.read(bytes, 0, size);
if (read < size) {
int remain = size - read;
while (remain > 0) {
read = fis.read(tmpBuff, 0, remain);
System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
remain -= read;
}
}
} catch (IOException e){
throw e;
} finally {
fis.close();
}
Log.v("bytes[]", bytes.toString());
return bytes;
}
In a class called WebConnection, I perform all http post methods needed to send a file.
Any suggestions of how I can call the FileChooserActivity's onFileSelected() method in the Add class to send the file (byte array form) via http post?
Thoughts but not successful because file is not being grabbed/referenced properly from FileChooserActivity so it is null:
In Add.class:
FileChooserActivity fca = new FileChooserActivity();
File file = null;
fca.onFileSelected(file);
Log.v("file", file.toString());
conn.addValuePair("a_file[]", file.toString());
As nochindeluxe stated, you should not instantiate an activity to simply call one method.
Following what he stated, in your activity add an interface that will allow you to set a listener callback, which will run when the action is completed. You could create a standalone listener interface or add one in the activity class as an inner class. I'm not really sure of the order things are happening here, but this should give you an idea of what to do.
public interface OnFileAddedListener {
// your method proto here.
void onFileAdded(Byte[] theFile);
}
Now create a member variable in the class you want the callback to be run from.
private OnFileAddedListener mListener;
Create a setter for the listener.
public void setOnFileAddedListener(OnFileAddedListener listener) {
mListener = listener;
}
Now, wherever you want this method to be executed (e.g. After a file is added), is where you should implement the callback.
if (mListener != null) {
mListener.onFileAdded(theByteArray);
}
Now, when you create the object, you can call the setter with something like this from your activity.
YourClass theObject = new YourClass();
// Set the call back from your activity.
theObject.setOnFileAddedListener(new OnFileAddedListener() {
@Override
public void onFileAdded(Byte[] theFile) {
// Send your file here, in the activity, via the callback listener, using your asynctask.
}
}
Hope this helps.