Can any one please tell me the process through which I can open a photo gallery in the blackberry application to choose a photo to upload, is there any file uploading control in the blackeberry 5.0 and also tell me the process to save the photo from the blackberry application to the remote server using HttpWebRequest.
Thanks
Their is nice way which i have done. Just get selected image from the phone through file browser or file io method than convert it to a byte array than just encode it to base64 String and send this string to the server by http request.
and at server side just do opposite.
final byte[] chunk;
chunk = new byte[actualSize];
try {
int bytesRead = in.read(chunk);
fconn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
String encodedStr = Base64OutputStream.encodeAsString(chunk, 0, chunk.length,false,false);
}
Related
I have been trying to implement an application to determine content type of any file. I use Apache Tika for determination.
Here is a basic code implementation for that:
InputStream fileStream = ContentTypeController.class.getClassLoader().getResourceAsStream(fileName);
Tika tika = new Tika();
String contentType = null;
try {
contentType = tika.detect(fileStream);
} catch (IOException e) {
e.printStackTrace();
}
Instead of code above I have to download files from Openstack to determine file content type. Some files are more than 100GB and downloading all file is heavy.
I can not figure out how to overcome this necessity of downloading all file, I hope you have any idea/solution without downloading all file
Tika has ability to determine content type of file without downloading all if you pass a URL parameter to detect() function.
Tika tika = new Tika();
String contentType = null;
try {
contentType = tika.detect(new URL("a url"));
} catch (IOException e) {
e.printStackTrace();
}
I'm having trouble trying to consume the Response of an HTTP Endpoint which Streams real-time events continously. It's actually one of Docker's endpoints: https://docs.docker.com/engine/api/v1.40/#operation/SystemEvents
I am using Apache HTTP Client 4.5.5 and it just halts indefinitely when I try to consume the content InputStream:
HttpEntity entity = resp.getEntity();
EntityUtils.consume(entity);//it just hangs here.
//Even if I don't call this method, Apache calls it automatically
//after running all my ResponseHandlers
Apparently, it can be done by using JDK's raw URL: Stream a HTTP response in Java
But I cannot do that since local Docker communicates over a Unix Socket which I only managed to configure in Apache's HTTP Client with a 3rd party library for Unix Sockets in Java.
If there is a smarter HTTP Client library which I could switch to, that would also be an option.
Any ideas would be greatly appreciated. Thank you!
I managed to solve this issue by generating an infinite java.util.stream.Stream of JsonObject from the response InputStream (I know the json reading part is not the most elegant solution but there is no better way with that API and also, Docker doesn't send any separator between the jsons).
final InputStream content = response.getEntity().getContent();
final Stream<JsonObject> stream = Stream.generate(
() -> {
JsonObject read = null;
try {
final byte[] tmp = new byte[4096];
while (content.read(tmp) != -1) {
try {
final JsonReader reader = Json.createReader(
new ByteArrayInputStream(tmp)
);
read = reader.readObject();
break;
} catch (final Exception exception) {
//Couldn't parse byte[] to Json,
//try to read more bytes.
}
}
} catch (final IOException ex) {
throw new IllegalStateException(
"IOException when reading streamed JsonObjects!"
);
}
return read;
}
).onClose(
() -> {
try {
((CloseableHttpResponse) response).close();
} catch (final IOException ex) {
//There is a bug in Apache HTTPClient, when closing
//an infinite InputStream: IOException is thrown
//because the client still tries to read the remainder
// of the closed Stream. We should ignore this case.
}
}
);
return stream;
I can able to upload single offline conversion to my adwords account through API.But I want to upload bulk conversions through API. Is there any way to upload bulk conversions in single API call. I am using v201402 adwords client library.
You could use the "MutateJobService" for this. This exactly serves your purpose
Here's the link:
https://developers.google.com/adwords/api/docs/reference/v201402/MutateJobService
https://developers.google.com/adwords/api/docs/guides/importing-conversions
Here is my c# code
public OfflineConversionFeedReturnValue UploadOfflineConversionsToExistingConversionType(List<OfflineConversionFeed> offlineConversions)
{
try
{
// Get the OfflineConversionFeedService.
OfflineConversionFeedService offlineConversionFeedService =
(OfflineConversionFeedService)User.GetService(
AdWordsService.v201509.OfflineConversionFeedService);
List<OfflineConversionFeedOperation> offlineConversionOperations = new List<OfflineConversionFeedOperation>();
foreach (OfflineConversionFeed conversion in offlineConversions)
{
OfflineConversionFeedOperation offlineConversionOperation =
new OfflineConversionFeedOperation();
offlineConversionOperation.#operator = Operator.ADD;
offlineConversionOperation.operand = conversion;
offlineConversionOperations.Add(offlineConversionOperation);
}
OfflineConversionFeedReturnValue offlineConversionRetval =
offlineConversionFeedService.mutate(offlineConversionOperations.ToArray());
return offlineConversionRetval;
}
catch (Exception e)
{
throw new System.ApplicationException("Failed upload offline conversions.", e);
}
}
could any one please help us to retrieve data from incoming message in blackberry 8520 and to store the retrived data in to the shared preferences
We can use the listener to listen the messages before coming into the inbox. But you cannot read the messages that are already received. You can use the below sample code:
try{
dc = (DatagramConnection) Connector.open("sms://");
for(;;){
if(stop){
return;
}
Datagram d = dc.newDatagram(dc.getMaximumLength());
dc.receive(d);
String address = new String(d.getAddress());
String msg = new String(d.getData());
MyScreen.update(msg,address);
}
}catch(Exception e){
System.err.println(e.toString());
}
You can find more information BlackBerry_Application_Developer_Guide_Volume_1 book
is there any way out that we can make the data in text file persistent? everytime a user finishes playing a game, in my program his name and respective score is written to a text file. When the next player comes the previous one gets overwritten. since am writing in write mode, I am not sure whether append mode is supported to save scores of this sort in blackberry...any suggestions are welcome
You should really use the PersistentStore to store this type of information - it's much easier to use and probably more reliable than trying to write files.
However, if you insist on writing files, here's the general code to open a file for appending:
private OutputStream openFileForWriting(String filePath) {
try {
FileConnection fconn = (FileConnection) Connector.open(filePath);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists()) {
fconn.create(); // create the file if it doesn't exist
}
return fconn.openOutputStream(fconn.fileSize());
} catch (IOException ioe) {
System.out.println("Could not open " + filePath + " for writing");
}
return null;
}