I want to add a limit on uploaded image size (currently images are uploaded to a server).
When anyone gets an image from SDcard or captures image from Camera, I want show message to user that it uploaded max file size - i.e 500kb or can I resize images into smaller sizes. For example 1mb image resize into 400-500kb (like Facebook).
Here is the sample code that I implemented after getting image from SDcard or captured image from Camera.
FileConnection file = (FileConnection)Connector.open(url);
if(file.exists())
{
try{
String fileName = url .substring(url.lastIndexOf('/') + 1);
//String fileName = url ;
Dialog.alert("fileName " + fileName);
InputStream inputStream = file.openInputStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int buffersize=1024;
byte[] buffer=new byte[buffersize];
int length=0;
while((length=inputStream.read(buffer))!=-1)
{
bos.write(buffer,0,length);
}
byte[] imagedata=bos.toByteArray();
Dialog.alert("Url " + Url + " Image Data Byte " + imagedata);
HttpConnection conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
String boundary = "Some_Unique_Text_Also_Alphanumeric";
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
+ ";boundary=" + boundary);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
String.valueOf(imagedata.length));
conn.setRequestProperty("x-rim-transcode-content", "none");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream finalOut = conn.openOutputStream();
String newLine = "\r\n";
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write(newLine.getBytes());
String contDisp = "Content-Disposition:form-data;name=\"image\";fileName=\"Image.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String contentType = "Content-Type:image/jpeg";
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(contentType.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(imagedata);
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write("--".getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());
out.flush();
out.close();
finalOut.flush();
finalOut.close();
InputStream instream=conn.openInputStream();
int ch=0;
StringBuffer buffesr=new StringBuffer();
while((ch=instream.read())!=-1)
{
buffesr.append((char)ch);
Dialog.alert("Uploaded");
}
}
catch (Exception e) {
Dialog.alert("Exception " + e);
}
}
Any help ??