0

My app run successfully on a Sony Experia (2.3), but I'm getting an exception while running it on a Samsung Galaxy Tab (4.1). My problem is that after recording video and converting it in base64 string, I'm getting an error while parsing it into soap.

This is my code for capturing video (passing intent):

Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);

videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);

startActivityForResult(videoIntent, ACTION_TAKE_VIDEO);

On Activity result method:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if ((requestCode == ACTION_TAKE_VIDEO && resultCode == RESULT_OK)) {
System.out.println("capturing video");
Uri selectedImage = data.getData();
String videoPath = getRealPathFromURI(selectedImage);

Bitmap bm = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.MINI_KIND);
videoview.setImageBitmap(bm);
System.out.println("videobitmap=="+bm);

bytes[]datas = null;
String dataPath = videoPath;
InputStream is = null;

try {
is = new FileInputStream(dataPath);
datas = readBytes(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

String encodedImage = Base64.encodeToString(datas, Base64.DEFAULT);
isInternetPresent = cd.isConnectingToInternet();

if (isInternetPresent) {
new BufferingVideo().execute(encodedImage);
} else {
Toast.makeText(getApplicationContext(), "Sorry, we couldn't connect to server, please check your 
internet connection", Toast.LENGTH_LONG).show();
}
}
}

public byte[] readBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;

while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}

return byteBuffer.toByteArray();
}

public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Now passing base64 string to Webservice:

protected String doInBackground(String... params) {
String st = params[0];
System.out.println("st==== " + st);

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME4);

System.err.println("Request  = " + request);
request.addProperty("VideoBuffer", st);
request.addProperty("VideoName", "VideoCar");
request.addProperty("ModuleName", modulename);
}

My Logcat error is:

FATAL EXCEPTION: AsyncTask #5
java.lang.RuntimeException: An error occured while executing doInBackground()
  at android.os.AsyncTask$3.done(AsyncTask.java:299)
  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  at java.lang.Thread.run(Thread.java:856)
  Caused by: java.lang.OutOfMemoryError
  at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
  at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
  at java.lang.StringBuffer.append(StringBuffer.java:219)
  at org.ksoap2.serialization.SoapObject.toString(SoapObject.java:456)
  at java.lang.StringBuilder.append(StringBuilder.java:202)
  at web.org.HouseRentInsertAds$BufferingVideo.doInBackground(HouseRentInsertAds.java:897)
  at web.org.HouseRentInsertAds$BufferingVideo.doInBackground(HouseRentInsertAds.java:1)
  at android.os.AsyncTask$2.call(AsyncTask.java:287)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

Kindly give me a solution please.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • possible duplicate of [How to increase heap size of an android application?](http://stackoverflow.com/questions/11275650/how-to-increase-heap-size-of-an-android-application) – JasonMArcher Aug 13 '14 at 18:01
  • Also related: http://stackoverflow.com/questions/5350465/android-heap-size-on-different-phones-devices-and-os-versions – JasonMArcher Aug 13 '14 at 18:01

1 Answers1

1

You have to compress the image befor sending whole image to server.

set public

String String_Image; 
Bitmap bitmap;

Here process of decoding the image and send compressed image to server

File image_file = new File(path);
decodeFile(image_file);         
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String_Image = Base64.encodeBytes(ba);

decodeFile() Function will decode your file.

private Bitmap decodeFile(File f)
    {
        try
        {
            // Decodes image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size to scale to
            final int REQUIRED_SIZE = 70;

            // Finds the correct scale value which should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                    && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decodes with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null,
                    o2);
            return bitmap;
        } catch (FileNotFoundException e)
        {
        }
        return null;
    }

Send String_Image to server.

This code is for image But same way you can do for other also. Try it with some changes.

Hope it will work.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • in lower version that can't handle too much memory to perform task.have you apply my concept ? – Chintan Khetiya Apr 15 '13 at 10:52
  • yes i hav applied... but decodeStream does not work its give out an error displaying null.. im using API level8(google API). – user1915881 Apr 16 '13 at 06:00
  • can u just specify some way clearly.... my question is does ecplise occupies too much space for allocating bitmap?if then is ther a constant pixel size that accepts(works )in all devices including tablet. – user1915881 Apr 16 '13 at 06:05