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);
}
Related
Edit: Ok, this is my last attempt at this, new code same result. I get a image in the ImageView but no sign of any return-string from the server.
This is what the server expects to get (The image can be url or base64 encoded image):
POST /detect HTTP/1.1
Content-Type: application/json
app_id: 4985f625
app_key: aa9e5d2ec3b00306b2d9588c3a25d68e
{
"image":" http://media.kairos.com/kairos-elizabeth.jpg ",
"selector":"ROLL"
}
And this is my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
EncodeToBase64 encode = new EncodeToBase64();
String lastPhotoAsBase64;
ImageView mImageView;
String path = Environment.getExternalStorageDirectory().toString() + "/files/Pictures";
static final int REQUEST_TAKE_PHOTO = 1;
String mCurrentPhotoPath;
String mTestPath = "/storage/emulated/0/Android/data/com.example.cliff.camera2/files/Pictures/JPEG_20171209_174301_1623885500.jpg";
String jsonResult;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.imageView);
Button cameraButton = findViewById(R.id.cameraButton);
textView = findViewById(R.id.textView);
cameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try {
setPic();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
public void setPic() throws IOException {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
//BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
ExifInterface exif = new ExifInterface(mCurrentPhotoPath);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
//int deg = rotationInDegrees;
Matrix matrix = new Matrix();
if (rotation != 0f) {
matrix.preRotate(rotationInDegrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
lastPhotoAsBase64 = encode.encode(bitmap);
if(lastPhotoAsBase64 != null) {
new postData().execute(lastPhotoAsBase64);
}
else{
Toast.makeText(MainActivity.this,"No base64 data found!",Toast.LENGTH_LONG).show();
}
mImageView.setImageBitmap(bitmap);
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
public class postData extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... strings) {
HttpConnectionActivity hh = new HttpConnectionActivity();
String temp = hh.HttpConnection(strings[0]);
textView.setText(temp);
textView.setVisibility(View.VISIBLE);
return null;
}
}
}
HttpConnectionActivity.java
public class HttpConnectionActivity {
private final static String KAIROS_URL = "https://api.kairos.com/detect";
private final String KEY = "5232cdde7e35d8de7ac78726060d5a01";
private final String ID = "9bd755a1";
String serverResponse;
public static final MediaType MEDIA_TYPE = MediaType.parse("application/json");
JSONObject postdata = new JSONObject();
public String HttpConnection(String image) {
OkHttpClient client = new OkHttpClient();
try {
postdata.put("image", image);
RequestBody body = RequestBody.create(MEDIA_TYPE, postdata.toString());
final Request request = new Request.Builder()
.url(KAIROS_URL)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("app_id", ID)
.addHeader("app_key", KEY)
.build();
Response response = client.newCall(request).execute();
serverResponse = response.body().string();
} catch (Exception ex) {
ex.getMessage();
}
return serverResponse;
}
}
EncodeToBase64.java
public class EncodeToBase64 {
public String encode(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 75, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}
}
OnCustomEventListener.java
public interface OnCustomEventListener {
public void onEvent();
}
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 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"
I'm using Volley library in my project but I have problem with OutOfMemory Exception. In my application I'm downloading thumbs and full size images from server via NetworkImageView using setImageUrl method. I'm using BitmapLruCache:
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public BitmapLruCache() {
this(getDefaultLruCacheSize());
}
public BitmapLruCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
#Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
#Override
public Bitmap getBitmap(String url) {
return get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
I'm getting OutOfMemoryException on HTC Desire (Android 2.2.2). How can I deal with this exception? Is something wrong I'm doing?
Edit
This exception I got during monkey test:
java.lang.OutOfMemoryError at
com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101)
at
com.android.volley.toolbox.PoolingByteArrayOutputStream.expand(PoolingByteArrayOutputStream.java:76)
at
com.android.volley.toolbox.PoolingByteArrayOutputStream.write(PoolingByteArrayOutputStream.java:84)
at
com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:213)
at
com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104)
at
com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
#Sipka - it doesn't solve my problem
#Muhammad Babar - Volley library handles all network/bitmap/cache operations so I need solution to fix OutOfMemory exception caused by Volley.
Use this code to create Bitmap in a thread that will help you
Bitmap bitmap = null;
HttpResponse response = null;
InputStream instream = null;
try {
File file = new File(Environment.getExternalStorageDirectory()
.toString(), floderName);
String s = file.getAbsolutePath();
f = new File(s);
if (!f.exists()) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(new URL(url[0]).toURI());
response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
return null;
}
// BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(
// response.getEntity());
instream = response.getEntity().getContent();
OutputStream os = new FileOutputStream(f);
Globals.CopyStream(instream, os);
os.close();
instream.close();
}
FileInputStream fs = null;
try {
fs = new FileInputStream(f);
} catch (FileNotFoundException e) {
// TODO do something intelligent
e.printStackTrace();
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inDither = false; // Disable Dithering mode
o2.inPurgeable = true; // Tell to gc that whether it needs free
// memory, the Bitmap can be cleared
o2.inInputShareable = true; // Which kind of reference will be used
// to recover the Bitmap data after
// being clear, when it will be used in
// the future
o2.inTempStorage = new byte[32 * 1024];
o2.inSampleSize = 1;
bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, o2);
bit = bitmap;
// bit.compress(Bitmap.CompressFormat.JPEG, 90, null);
newsFeed.setBitmap(bit);
// Data.globelCoverIcon = bit;
// OutputStream os = new FileOutputStream(f);
} catch (Exception ex) {
ex.printStackTrace();
}
public class Globals {
private static final int JPEG_EOI_1 = 0xFF;
private static final int JPEG_EOI_2 = 0xD9;
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
Log.e("App", ex.getMessage(), ex);
}
}
}
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();
}
}