Last byte[] only send to the server by using MultipartEntity in android - android

I have implemented the multipleimage uploading.If i select three value means,all three images are converted into byte[] perfectly but last value only send into server.How to send all three images.I have tried like this
int i=0;
for ( i = 0; i < ImgData.size(); i++) {
Log.d("ImgData(i)--", String.valueOf(ImgData.get(i)));
mImageIds = new ArrayList<String>();
Bitmap bitmap = decodeFile(ImgData.get(i));
String image = getStringImage(bitmap);
mImageIds.add(image);
Log.d("Image--", image);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray;
byteArray = stream.toByteArray();
Log.d("byteArray11--", String.valueOf(byteArray));
// ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image");
entity.addPart("room_images", new ByteArrayBody(byteArray,
"image/jpeg"));
// Add a bitmap
}

Related

compress a bitmap and save it to a bitmap object

I am a beginner in android. I decoded a Base64 image to Bitmap and i need to compress it to to an Outputstream, now i need to use the Bitmap get added to the ArrayList.
try {
// Getting JSON Array from URL
details = json.getJSONArray(TAG_Root);
for(int i = 0; i < details.length(); i++){
JSONObject c = details.getJSONObject(i);
//Storing JSON item in a Variable
String branch = c.getString(TAG_Branch);
String address = c.getString(TAG_Add);
String uname = c.getString(TAG_User);
String photo = c.getString(TAG_Photo);
System.out.println(photo);
//decoding and compressing bitmap
byte[] imageAsBytes = Base64.decode(photo, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
//bitmap = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
//Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
al_branch.add(branch);
al_add.add(address);
al_user.add(uname);
al_photo.add(bitmap);
}
System.out.println(al_branch);
System.out.println(al_photo);
custom_list adapter = new custom_list(MainActivity.this, al_branch,al_user,al_add ,al_photo);

base64 string is not uploaded in server android?

I need to send the image (from gallery) to server in the format of base64 string. I get the path of the image and converted it to base64 string. but at server the image is not showing fully. only 10% of the image is showing at server side.Please any one help me how to convert the image to base64 string.
code:
filePath = cursor.getString(columnIndex);
Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
ba1 =Base64.encodeToString(ba, Base64.DEFAULT);
image size:460kb
try with this i hope it will work for you and send using httpPost method.
public static String BitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
return base64;
}
and server side.
Base64 base64=new Base64();
saveImage(base64.decode(profilePic),"yourName");
public void saveImage(byte[] bytes,String name){
String path="D:/"+name.hashCode()+".png";
try {
FileOutputStream f = new FileOutputStream(path);
f.write(bytes);
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}

how to upload image in base64 on server

i have a problem, i am uploading image on server but it is not. i have convert image in base64 and get through json. but json is not properly closed due to this i m getting error. error id om postimafe variable. in this variable {"key"""encode, here is json is not closed.
// code for convert base64
public static String getBase64String(String baseFileUri)
{
String encodedImageData = "";
try
{
System.out.println("getBase64String method is called :" +baseFileUri);
Bitmap bm = BitmapFactory.decodeFile(baseFileUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImageData = Base64.encodeToString(b, Base64.DEFAULT);
//ArrayList<NameValuePair> imagearraylistvalue = new ArrayList<NameValuePair>();
//imagearraylistvalue.add(new BasicNameValuePair("image", encodedImage));
System.out.println("encode data in upload file :" +encodedImageData );
}
catch(Exception ex)
{
System.out.println("Exception in getBase64String method in Utility class :" +ex);
}
return encodedImageData ;
}
// code for json and uplod base64 to server but i m getting error
System.out.println("fullupload image for 1:" +fulluploadimgpath);
String base64String = Utility.getBase64String(fulluploadimgpath);
System.out.println("base64String is in :" +base64String);
if (base64String != null)
{
JSONObject postImageData = new JSONObject();
postImageData.put("media",base64String);
System.out.println("post image :" +postImageData);
HttpResponse imgPostResponse = Utility.postDataOnUrl(Utility.getBaseUrl()+"user/upload",obj.toString());
System.out.println("fullupload image for imgPostResponse:" +imgPostResponse);
if (imgPostResponse != null)
{
String imgResponse = Utility.readUrlResponseAsString(imgPostResponse);
System.out.println("imgResponse is in imgResponse :" +imgResponse);
if (imgResponse != null|| imgResponse.trim().length() != 0)
{
JSONObject jResObj = new JSONObject();
if (jResObj.getBoolean("rc"))
{
obj.put(hidobj.getReceiveAs(),jResObj.getLong("ident"));
}
}
String encodedImageData =getEncoded64ImageStringFromBitmap(your bitmap);
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}

Android Byte Array to Bitmap Results Null After Receiving From Socket

I have created a server which receives byte array from a c++ client, the client send image as uchar array(using opencv) and on the android I am receiving the data correctly. The server on android store data to byte array and I need to convert this byte array to Bitmap. But I am getting null Bitmap after using BitmapFactory.decodeByteArray.
Here is my server code which receives data and store in to byte array
class imageReciver extends Thread {
public static byte imageByte[];
private ServerSocket serverSocket;
InputStream in;
int imageSize=921600;//expected image size 640X480X3
public imageReciver(int port) throws IOException{
serverSocket = new ServerSocket(port);
}
public void run()
{
Socket server = null;
server = serverSocket.accept();
in = server.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int remainingBytes = imageSize; //
while (remainingBytes > 0) {
int bytesRead = in.read(buffer);
if (bytesRead < 0) {
throw new IOException("Unexpected end of data");
}
baos.write(buffer, 0, bytesRead);
remainingBytes -= bytesRead;
}
in.close();
imageByte = baos.toByteArray();
baos.close();
server.close();
//Here conver byte array to bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(imageByte, 0,imageByte.length);
return;
}
}
I seems your code is not correct, try this:
try {
URL myURL = new URL(url);
final BufferedInputStream bis = new BufferedInputStream(myURL.openStream(), 1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
BufferedOutputStream out = new BufferedOutputStream(dataStream,1024);
copy(bis, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length, bfo);
bis.close();
//use the bitmap...
}
catch (Exception e) {
//handle ex
}

how can i code for capture camera and then convert it into the pdf format in the same camera activity class?

my code for storing image into sd card as follow:
public static boolean StoreByteImage(Context mContext, byte[] imageData,int quality, String expName)
{
File sdImageMainDirectory = new File("/sdcard");
FileOutputStream fileOutputStream = null;
String nameFile;
try
{
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,imageData.length,options);
fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/image/*.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myImage.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
}
you need to download iText library from here
http://www.itextpdf.com/
and then follow this to convert image to PDFConvert image to PDF in Android
image from camera
//Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
imagebytes = stream.toByteArray();
stream.close();
stream = null;
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILEPATH));
document.open();
Image image = Image.getInstance(imagebytes);
image.scaleAbsolute(imageBitmap.getWidth(),
imageBitmap.getHeight());
image.setAlignment(Image.MIDDLE |Image.ALIGN_MIDDLE);
document.add(image);
document.close();
File file = new File("mnt/sdcard/myPdf.pdf");
byte[] binaryFile = new byte[(int) file.length()];
InputStream fis = new FileInputStream(file);
fis.read(binaryFile);
fis.close();

Categories

Resources