I want to set an image in ImageView, I am retrieving the image path in my first activity and I am passing it via Intent as a String to second activity. In the second activity I set the path to an ImageView. It's working properly, and I need to upload that picture to a server. So I decoded the path to a bitmap. It throws an OutOfMemoryError. How to resolve this issue?
And when I use front camera, there is no issues. Image is uploaded successfully. The problem is with the images taken by front camera of the device. What is the solution for this problem? Can anyone help?
Here is the code to convert the image path to a string and passing it via Intent:
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, MediaStore.Images.Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
do {
Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
photoPath = uri.toString();
}while(cursor.moveToNext());
cursor.close();
try {
Intent intent = new Intent(MainActivity.this, ImageUploadActivity.class);
intent.putExtra("ImagePath", photoPath);
MainActivity.this.startActivity(intent);
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "Method invoked"+photoPath, Toast.LENGTH_SHORT).show();
}
}
Receiving Intent in Second Activity:
Intent camIntent = getIntent();
camPicPath = camIntent.getExtras().getString("ImagePath");
imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(camPicPath));
Toast.makeText(getApplicationContext(), "PATHe"+camPicPath, Toast.LENGTH_SHORT).show();
bitmap = (BitmapFactory.decodeFile(camPicPath));
Method to Upload the file:
class ImageUploadTask extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... unsued) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://11.10.11.15/test/upload.php");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
/* entity.addPart("uploaded_file", new ByteArrayBody(data,
"myImage.jpg"));*/
// String newFilename= filename.concat("file");
// newFilename=filename+newFilename;
entity.addPart("uploaded_file", new ByteArrayBody(data,
filename));
// Log.e(TAG, "Method invoked");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
String aux = "";
while ((aux = reader.readLine()) != null) {
builder.append(aux);
}
String sResponse = builder.toString();
return sResponse;
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Exception Message 1", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
return null;
}
Use the following method:
Bitmap bm = ShrinkBitmap(imagefile, 300, 300);
image.setImageBitmap(bm);
Bitmap ShrinkBitmap(String file, int width, int height) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
Or use inSampleSize when setting the image bitmap like this:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
imageView.setImageBitmap(BitmapFactory.decodeFile(path, options));
you can add below property to application tag of manifiest file for high memory.
android:largeHeap="true"
Related
I've written a code that selects image from gallery and convert it into a BASE64 string. as the selected images was too large the string is too big to be posted.
I have compressed the image so that the length of the string could be reduced. but still the length of the string is still large.
The code that i used is as follows,
This function sets the selected image in an imageView and
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
sPicturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bm = ShrinkBitmap(sPicturePath, 300, 300);
imageView.setImageBitmap(bm);
/**
* Compute size of the image selected image
*/
File file = new File(sPicturePath);
if (file.exists()) {
double bytes = file.length();
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);
System.out.println("megabytes : " + megabytes);
Log.d("size", String.valueOf(megabytes));
}
imageView.setImageBitmap(BitmapFactory.decodeFile(sPicturePath));
Bitmap bm1 = BitmapFactory.decodeFile(sPicturePath);
//CropImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm1.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] byteArrayImage = baos.toByteArray();
encodedString = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
//Toast.makeText(getApplicationContext(), encodedString, Toast.LENGTH_SHORT).show();
String length = String.valueOf(encodedString.length());
//Toast.makeText(getApplicationContext(),lenght,Toast.LENGTH_SHORT).show();
Log.d("encodedString", encodedString);
Log.d("length", length);
ShrinkBitmap.java
Bitmap ShrinkBitmap(String file, int width, int height) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
What i want to achieve is to select an image from gallery convert it into a BASE64 string and post it through a REST service.
A GET request has URL length restrictions. You need to send the file using MultipartEntity using HttpURLConnection. You need to create a post request.
If your filename is image.jpg.
Bitmap bitmap = ...;
String filename = "image.jpg";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
ContentBody contentPart = new ByteArrayBody(bos.toByteArray(), filename);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("picture", contentPart);
String response = multipost("http://server.com", reqEntity);
And this is the multipost function.
private static String multipost(String urlString, MultipartEntity reqEntity) {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.addRequestProperty("Content-length", reqEntity.getContentLength()+"");
conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
OutputStream os = conn.getOutputStream();
reqEntity.writeTo(conn.getOutputStream());
os.close();
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(conn.getInputStream());
}
} catch (Exception e) {
Log.e(TAG, "multipart post error " + e + "(" + urlString + ")");
}
return null;
}
private static String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return builder.toString();
}
More info at this SO thread.
I am trying to get an image and upload it. This is always working for Camera but not working for images from gallery. It is failing with HTTP status of 422 and always succeeds for camera images with status code 201.
Here is my Image capture code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap takenPictureData;
switch (imageUploadMethod) {
case Constants.SELECT_CAMERA:
try {
if (requestCode == Constants.SELECT_CAMERA && data != null) {
Bundle extras = data.getExtras();
if (extras!= null && extras.containsKey("data")) {
takenPictureData = (Bitmap) extras.get("data");
ImageView imageView = (ImageView) getActivity().findViewById(R.id.fragment_add_retailer_img_pic);
imageView.setImageBitmap(takenPictureData);
uploadImage(takenPictureData, false, retailer_profile_client_transacation_id);
Log.d("IMAGE_ISSUE", "IMAGE BITMAP : " + takenPictureData);
}
}
} catch (Exception e) {
Log.d("Exception", "" + e.toString());
}
break;
case Constants.SELECT_GALLERY:
if (data != null) {
Uri selectedImageUri = data.getData();
takenPictureData = ImageUtils.getBitmapFromUri(getActivity(), selectedImageUri);
picCallBackImageView1.setImageBitmap(takenPictureData);
uploadImage(takenPictureData, false, retailer_profile_client_transacation_id);
Log.d("IMAGE_ISSUE", "IMAGE BITMAP : " + takenPictureData);
}
break;
}
This is the utility method:
public static Bitmap getBitmapFromUri(Context c, Uri uri) {
Bitmap image = null;
try {
ParcelFileDescriptor parcelFileDescriptor =
c.getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
}
catch(IOException e){
if (Constants.PRINT_DEBUG) {
e.printStackTrace();
Log.d("URI to Bitmap", "" + e.toString());
}
}
return image;
}
After getting the Bitmap, I am passing the byte array to my task
// convert from bitmap to byte array
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
And here is my HttpRequestImageUpload.java
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class HttpRequestImageUpload extends AsyncTask<byte[], Void, String> {
Context context;
private IRequestCallback callback;
SuperActivity superActivity;
SuperFragment superFragment;
HttpPut httpPut;
int client_transaction_id;
String str;
public IRequestCallback getCallback() {
return callback;
}
public void setCallback(IRequestCallback callback) {
this.callback = callback;
}
public HttpRequestImageUpload(SuperActivity superActivity, Context context , int client_transaction_id) {
this.superActivity = superActivity;
this.context = context;
this.client_transaction_id = client_transaction_id;
}
public HttpRequestImageUpload(SuperFragment superFragment, Context context , int client_transaction_id) {
this.superFragment = superFragment;
this.context = context;
this.client_transaction_id = client_transaction_id;
}
#Override
protected String doInBackground(byte[]... params) {
Log.d("IMAGE_ISSUE", "SENT FROM doInBackground() : " + params[0]);
return upload(params[0]);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("IMAGE_ISSUE", "On postExeceute() : " + s);
if (s.equalsIgnoreCase("Error")) {
callback.errorCallBack(s,str);
} else {
callback.imageUploadCallBack(s,str);
}
}
#Override
protected void onCancelled() {
Log.d("IMAGE_ISSUE", "Cancelled in ImageUpload");
try {
if(httpPut != null) {
httpPut.abort();
}
} catch (Exception e) {
Crashlytics.logException(e);
}
super.onCancelled();
}
#Override
protected void onCancelled(String s) {
super.onCancelled(s);
}
public String upload(byte[] byteArrayEntity) {
setCallback(superActivity != null ? superActivity : superFragment);
Log.d("IMAGE_ISSUE", "UPLOADING : request " + byteArrayEntity);
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 120000);
HttpConnectionParams.setSoTimeout(client.getParams(), 120000);
httpPut = new HttpPut(CommonUtil.getBaseUrl()+"artefacts?type=Image&client_transaction_id="+client_transaction_id);
httpPut.addHeader("Authorization", "Bearer " + Prefs.getToken(context));
httpPut.addHeader("Content-Type", "application/octet-stream");
httpPut.setEntity(new ByteArrayEntity(byteArrayEntity));
try {
HttpResponse response = client.execute(httpPut);
Log.d("IMAGE_ISSUE", "UPLOADING : Response " + response);
StatusLine statusLine = response.getStatusLine();
Log.d("IMAGE_ISSUE", "UPLOADING : Status Line " + statusLine);
int statusCode = statusLine.getStatusCode();
Log.d("IMAGE_ISSUE", String.valueOf(statusCode));
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} else if (statusCode == 201) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} else {
return ("Error");
}
} catch (Exception e) {
Log.d("IMAGE_ISSUE", "Exception: " + e.toString());
}
return stringBuilder.toString();
}
}
Well...as I said ...this async task works well for all images captured from camera. But in case of images taken from gallery.... the async task is not executing with 422 status.
This is a very common mistake..I don't know why I did not find any good resources to solve this issue...after looking around it for a few hours...I find that the When i am uploading image from gallery...the size comes out to be much larger than when i capture a bitmap using Camera. The difference is around 50 times larger size for gallery... this is causing an issue with ImageUpload...resulting into 422 Status.
Scaling the image in OnActivityResult() and then sending it to the ImageUploadAsyncTask... worked like charm. Here is the method I used to scale the bitmap.
public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException {
InputStream is = context.getContentResolver().openInputStream(photoUri);
BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, dbo);
is.close();
int rotatedWidth, rotatedHeight;
int orientation = getOrientation(context, photoUri);
if (orientation == 90 || orientation == 270) {
rotatedWidth = dbo.outHeight;
rotatedHeight = dbo.outWidth;
} else {
rotatedWidth = dbo.outWidth;
rotatedHeight = dbo.outHeight;
}
Bitmap srcBitmap;
is = context.getContentResolver().openInputStream(photoUri);
if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
float maxRatio = Math.max(widthRatio, heightRatio);
// Create the bitmap from file
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) maxRatio;
srcBitmap = BitmapFactory.decodeStream(is, null, options);
} else {
srcBitmap = BitmapFactory.decodeStream(is);
}
is.close();
/*
* if the orientation is not 0 (or -1, which means we don't know), we
* have to do a rotation.
*/
if (orientation > 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
srcBitmap.getHeight(), matrix, true);
}
String type = context.getContentResolver().getType(photoUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (type.equals("image/png")) {
srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
} else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
}
byte[] bMapArray = baos.toByteArray();
baos.close();
return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
}
Please excuse my bad English, I'm French !
In my Android App, I have to resize and crop a picture from the gallery before sending it to a server WITHOUT saving it.
Here my code to send to the server :
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String pathToOurFile = imagePath;
String urlServer = "http://ip/serverApp/upload/transfert.php";
Log.e("UploadImage", urlServer);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
File file = new File(imagePath);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
fileInputStream.close();
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
connection.setDoOutput(true);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
int bufferLength = 1024;
for (int i = 0; i < bytes.length; i += bufferLength) {
int progress = (int)((i / (float) bytes.length) * 100);
publishProgress(progress);
if (bytes.length - i >= bufferLength) {
outputStream.write(bytes, i, bufferLength);
} else {
outputStream.write(bytes, i, bytes.length - i);
}
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
outputStream.close();
outputStream.flush();
InputStream inputStream = connection.getInputStream();
// read the response
inputStream.close();
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.w("Upload image", "Response -> Code:"+serverResponseCode+" Message:"+serverResponseMessage);
return serverResponseCode;
}
catch (Exception ex)
{
ex.printStackTrace();
}
Now I need to code to resize and crop the picture in order to have image of a size of 350px/350px.
Do you know how I could do that ?
Thanks a lot.
OK !!!!
For the correct way follow this this code !
BUT : becarful, it's an example ! -> YOU SHOULDN'T DO A INTERNET REQUEST IN THE MAIN THREAD
for execute this code, the function exec(); should to be put into a "doInBackground()" of an asyncTask<Object, Object, Object>();
the startActivityForResult() and the override of onActivityResult() should to be into an activity class
tell me if it's correct !!!!
private int ACTIVITY_ID_PICK_PHOTO = 42;
private int maxWidth = 350;
private int maxHeight = 350;
private String url = "http://ip/serverApp/upload/transfert.php"
//Call the activity for select photo into the gallery
private void SelectPhoto(){
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ACTIVITY_ID_PICK_PHOTO);
}
// check the return of the result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//check th id of the result
if (requestCode == ACTIVITY_ID_PICK_PHOTO)
selectPhotoControl(data);
}
//Working data
private void selectPhotoControl(Intent data) {
//check if any photo is selected
if (data == null)
return;
//get the uri of the picture selected
Uri photoUri = data.getData();
if (photoUri != null) {
//decode the Uri
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(photoUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//get the uri of the image
String filePath = cursor.getString(columnIndex);
cursor.close();
//get the image in the bitmap and resize the image
Bitmap bp = resize(filePath);
if (bp != null)
postImage(bp, filePath);
}
}
public static HttpResponse postImage(Bitmap bp, String uristr) throws ClientProtocolException, IOException {
//initialization of the postrequest
HttpPost httpPost = new HttpPost(url);
//create the multipart entitiy (if you want send another content)
MultipartEntity entity = new MultipartEntity(
//the boundary for separate the informations
HttpMultipartMode.BROWSER_COMPATIBLE, "------CustomBoundary", null);
if (bp != null) {
//create the bytes array for send the image
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//if you want to compress the image -> write the result into bos
bp.compress(CompressFormat.JPEG, 100, bos);
//get the filename of the image
String filename = uristr.substring(uristr.lastIndexOf("/") + 1,
uristr.length());
//put the picture into the body of this part
FormBodyPart fbp = new FormBodyPart("photo", new ByteArrayBody(
bos.toByteArray(), "image/jpeg", filename));
//add the part to the entity
entity.addPart(fbp);
}
//set the entity into the request
httpPost.setEntity(entity);
//execute the request
return exec(httpPost);
}
protected synchronized static HttpResponse exec(HttpRequestBase base) throws ClientProtocolException, IOException{
if (base != null)
//Execute the request
return mHttpClient.execute(base);
else
return null;
}
private Bitmap resize(String path){
// create the options
BitmapFactory.Options opts = new BitmapFactory.Options();
//just decode the file
opts.inJustDecodeBounds = true;
Bitmap bp = BitmapFactory.decodeFile(path, opts);
//get the original size
int orignalHeight = opts.outHeight;
int orignalWidth = opts.outWidth;
//initialization of the scale
int resizeScale = 1;
//get the good scale
if ( orignalWidth > maxWidth || orignalHeight > maxHeight ) {
final int heightRatio = Math.round((float) orignalHeight / (float) maxHeight);
final int widthRatio = Math.round((float) orignalWidth / (float) maxWidth);
resizeScale = heightRatio < widthRatio ? heightRatio : widthRatio;
}
//put the scale instruction (1 -> scale to (1/1); 8-> scale to 1/8)
opts.inSampleSize = resizeScale;
opts.inJustDecodeBounds = false;
//get the futur size of the bitmap
int bmSize = (orignalWidth / resizeScale) * (orignalHeight / resizeScale) * 4;
//check if it's possible to store into the vm java the picture
if ( Runtime.getRuntime().freeMemory() > bmSize ) {
//decode the file
bp = BitmapFactory.decodeFile(path, opts);
} else
return null;
return bp;
}
This answer [is based on another answer][1], but I had some problems with that code, so I post the edited and working code.
I did it like this:
BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth = 50; //pixels
options.outHeight = 50; //pixels
InputStream in = context.getContentResolver().openInputStream(data.getData()); // here, you need to get your context.
Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bitmapdata = baos.toByteArray();
Notice that, data is the returned data from the Intent you use to get the file. If you already have the file path, just use that...
And now, when you are creating the HTTP Entity, add:
FormBodyPart fbp = new FormBodyPart("image", new ByteArrayBody(baos.toByteArray(), "image/jpeg", "image"));
entity.addPart(fbp);
Also, notice that you need a MultipartEntity to upload files.
I am sending Images and Text to a PHP webservice using the following code.
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(URL);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
entity.addPart("files[]",
new ByteArrayBody(data, "myImage.jpg"));
entity.addPart("message0", new StringBody(caption.getText()
.toString()));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
return sResponse;
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
Toast.makeText(ImageUpload.this, e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
return null;
}
}
It works perfectly. But this is only for one image. I want to send 5 images.
Example: Image1 - Text1
Image2 - Text2 etc..
So I am confused about how to store 5 images one by one and then on button click, send these images and text associated with them to the server.
I am getting images from the phone's camera.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE);
public void onActivityResult_photo(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK) {
if (data != null) {
mImageCaptureUri = data.getData();
display(mImageCaptureUri);
} else {
Toast.makeText(CustomTabActivity.mTabHost.getContext(),
"No photo selected..", Toast.LENGTH_SHORT).show();
}
}
}
private String display(Uri mImageCaptureUri2) {
// TODO Auto-generated method stub
String base64string = null;
try {
if (mImageCaptureUri2 != null) {
System.gc();
selectedImagePath = getPath(mImageCaptureUri2);
File filenew = new File(selectedImagePath);
int file_size = Integer.parseInt(String.valueOf(filenew
.length() / 1024));
if (file_size <= 10000) {
PD1 = ProgressDialog.show(
CustomTabActivity.mTabHost.getContext(), "",
"Loading...");
Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run() {
PD1.setCancelable(true);
Bitmap newbitmap;
newbitmap = decodeFile(selectedImagePath);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
newbitmap.compress(Bitmap.CompressFormat.PNG, 50,
bs);
img.setVisibility(View.VISIBLE);
img.setImageBitmap(newbitmap);
byte[] abc = bitmapToByteArray(newbitmap);
if (txt_phototext.getText().toString().equals("")) {
submit.put(abc, "");
} else {
submit.put(abc, txt_phototext.getText()
.toString());
// executeMultipartPost();
}
PD1.dismiss();
}
});
} else {
AlertDialog.Builder alertbox = new AlertDialog.Builder(
CustomTabActivity.mTabHost.getContext());
alertbox.setMessage("Take Image Size Less than 10 MB");
alertbox.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
finish();
}
});
alertbox.show();
}
} else {
System.out.println("===============NULL========");
}
} catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
}
return base64string;
}
static Bitmap decodeFile(String str) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(str), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(str), null,
o2);
} catch (FileNotFoundException e) {
}
return null;
}
public static byte[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
And make sure that your directory or folder in server is Executable, Writable and Readable. I had this as the major problem. This is called 777 permission.. Believe me, this is as important as other things to consider.
For full detail please have a look on my post Click here
its quite difficult to send multiple images to server using MultipartEntity. I did search for this but didn't find any right solution then i made my own way to send multiple images to server
, here i send array of selected paths to asynctask and in asynctask i sent images to server
Calling Asysnctask Function-
new Upload_Multiple.excute(Array_of_Path[]))
Private class Upload_Multiple_img extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
protected String doInBackground(String... paths_array) {
String data = "";
for (int i = 0; i < paths_array.length; i++) {
// get_Picture_bitmap() returns bitmap by passing path of image
// get_Picture_bitmap() is mentioned below.
Bitmap bitmap = get_Picture_bitmap(paths_array[i]);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert
DefaultHttpClient httpclient = new DefaultHttpClient();
String server_funtion_url="...serveraddres"+funtion_at_server"";
HttpPost httppost = new HttpPost(server_funtion_url); // server
MultipartEntity reqEntity = new MultipartEntity();
obj_SP = ImagePicker.this.getSharedPreferences("Eperty", 0);
String id_prop = obj_SP.getString("new_prop_id", "");
String Image_Name =
+ String.valueOf(System.currentTimeMillis()) + ".jpg";
// image is a key which is used at server end to get this
reqEntity.addPart("image", Image_Name, in);
httppost.setEntity(reqEntity);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());
System.out.println("FFFF== " + data);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
ConstantData.ToastAlert(ImagePicker.this,
"Images Uploaded successfully");
}
}
//);
For compressing the images and getting bitmap for i made below funtion*
public Bitmap get_Picture_bitmap(String imagePath) {
long size_file = getFileSize(new File(imagePath));
size_file = (size_file) / 1000;// in Kb now
int ample_size = 1;
if (size_file <= 250) {
System.out.println("SSSSS1111= " + size_file);
ample_size = 2;
} else if (size_file > 251 && size_file < 1500) {
System.out.println("SSSSS2222= " + size_file);
ample_size = 4;
} else if (size_file >= 1500 && size_file < 3000) {
System.out.println("SSSSS3333= " + size_file);
ample_size = 8;
} else if (size_file >= 3000 && size_file <= 4500) {
System.out.println("SSSSS4444= " + size_file);
ample_size = 12;
} else if (size_file >= 4500) {
System.out.println("SSSSS4444= " + size_file);
ample_size = 16;
}
Bitmap bitmap = null;
BitmapFactory.Options bitoption = new BitmapFactory.Options();
bitoption.inSampleSize = ample_size;
Bitmap bitmapPhoto = BitmapFactory.decodeFile(imagePath, bitoption);
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
int orientation = exif
.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if ((orientation == 3)) {
matrix.postRotate(180);
bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
true);
} else if (orientation == 6) {
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
true);
} else if (orientation == 8) {
matrix.postRotate(270);
bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
true);
} else {
matrix.postRotate(0);
bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
true);
}
return bitmap;
}
**
Server end Code *
$target_dir = "../webadmin/user_image/";
$target_dir = $target_dir . basename($_FILES["user_img"]["name"]);
if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_dir))
{
$msg = "The file ". basename($result[0]). " has been uploaded.";
$send_arr['success'] = 1;
$send_arr['message'] = $msg;
echo json_encode($send_arr);
}
else
{
$msg = "Sorry, there was an error uploading your file.";
$send_arr['success'] = 0;
$send_arr['message'] = $msg;
echo json_encode($send_arr);
}
Why you can't just create array of json object of your images to base64 and post to server and at your server api read those images convert to byte and use as image.
Check my answe and try to implement.
In Android how to post data to webservice which is created in WCF?
And the images you are getting from camera store them in uri in sdcard and letter read them. You can assign image name sequntialy. And read them from uri.
Try increasing the post_max_size of your php.ini file in WAMP server
Please find the below method...here i m sending mutiple image file using AQUERY. The best lib to perform all background network related task.(Like AJAX).
https://code.google.com/p/android-query/
public void uploadImageFile( String filePath,
String message) {
Context context = ApplicationContextProvider.getContext();
String url = SERVER_URL + "/user/uploadImageFile";
try {
Toast.makeText(context, "Uploading...", Toast.LENGTH_SHORT)
.show();
String compressedFile = CommonUtilities.compressImage(filePath,
context);
Map<String, Object> params = new HashMap<String, Object>();
File imageFile = new File(compressedFile);
byte[] imageBytes1 = FileUtils.readFileToByteArray(imageFile);
params.put("imageBytes", imageBytes1);
params.put("message",URLEncoder.encode(message, "UTF-8"));
AQuery aq = new AQuery(context);
aq.ajax(url, params, JSONObject.class,
new AjaxCallback<JSONObject>() {
#Override
public void callback(String url, JSONObject json,
AjaxStatus status) {
Toast.makeText(
ApplicationContextProvider.getContext(),
"Uploaded successfully",
Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
I am using following code to display bitmap in my ImageView.
When I try to load image of size for example bigger than 1.5MB it give me error.
Any one suggest me solution?
try {
URL aURL = new URL(myRemoteImages[val]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = null;
try
{
is= conn.getInputStream();
}catch(IOException e)
{
return 0;
}
int a= conn.getConnectTimeout();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm;
try
{
bm = BitmapFactory.decodeStream(bis);
}catch(Exception ex)
{
bis.close();
is.close();
return 0;
}
bis.close();
is.close();
img.setImageBitmap(bm);
} catch (IOException e) {
return 0;
}
return 1;
Log cat:
06-14 12:03:11.701: ERROR/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
06-14 12:03:11.861: ERROR/AndroidRuntime(443): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-14 12:03:11.861: ERROR/AndroidRuntime(443): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
You should decode with inSampleSize option to reduce memory consumption. Strange out of memory issue while loading an image to a Bitmap object
Another option inJustDecodeBounds can help you to find correct inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a
In general I think this blog covers the best practices on how to watch memory allocation/ how to use Weak/Soft References to avoid overflows.
Hope this helps.
try {
Bitmap bitmap=null;
byte[] profileImageInBytes;
String url="http://photo.net/learn/collage/complete-full-size.jpg";
HttpGet httpRequest = null;
httpRequest = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
System.gc();
Runtime.getRuntime().gc();
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
bmpFactoryOptions.inTempStorage = new byte[32 * 1024];
bmpFactoryOptions.inSampleSize = 4;
bmpFactoryOptions.outWidth = 640;
bmpFactoryOptions.outHeight = 480;
bmpFactoryOptions.inDither=false;
bmpFactoryOptions.inInputShareable=true;
bitmap = BitmapFactory.decodeStream(instream, new Rect(), bmpFactoryOptions);
System.out.println("hi " +bitmap);
Bitmap map = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
System.out.println("23");
System.out.println("hihi hi " +map);
BitmapDrawable bmd = new BitmapDrawable(map);
System.out.println("24");
System.out.println("hihi hi " +bmd);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
System.out.println(stream);
map.compress(Bitmap.CompressFormat.JPEG, 100, stream);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) 400);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) 400);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
Bundle params=new Bundle();
params.putString("method", "photos.upload");
profileImageInBytes = stream.toByteArray();
System.out.println(profileImageInBytes);
System.out.println(" profile image bytes ");
System.out.println("Bytes : " + profileImageInBytes);
params.putByteArray("picture", profileImageInBytes);
System.out.println("My Picture : " + params);
mAsyncRunner.request(null, params, "POST",
new SampleUploadListener(), null);
System.out.println("Uploading");
}
catch (IOException e) {
e.printStackTrace();
}