0

hello I have this error in the moment of i would like transfert a image in android. I don't understand why.

EDIT: I have find the raison.The raison are they have insuffisant allowed Memory. How to change the allowed memory?

   EDIT:FATAL EXCEPTION: main
   java.lang.OutOfMemoryError

07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at P12.transferImage(P12.java:503)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at P12.access$4(P12.java:487)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at P12$6.onClick(P12.java:313)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.view.View.performClick(View.java:3644)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.view.View$PerformClick.run(View.java:14313)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.os.Handler.handleCallback(Handler.java:605)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.os.Looper.loop(Looper.java:137)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at android.app.ActivityThread.main(ActivityThread.java:4514)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-18 08:50:35.169: E/AndroidRuntime(16772):    at dalvik.system.NativeStart.main(Native Method)
07-18 08:55:44.474: I/Process(16772): Sending signal. PID: 16772 SIG: 9
07-18 08:55:44.764: D/dalvikvm(17129): GC_FOR_ALLOC freed 65K, 4% free 12756K/13187K, paused 16ms

The problem's are in line

Bitmap imgTaken = BitmapFactory.decodeFile(from, options);

The url file are correct I have verified

This are the begin of my transfert function.

private void transferImage(String from,String imageName) throws IOException {
    Log.i("debug","from:"+from.toString());
    // we want remote host, user name and password
    if (from == null || from.length() == 0) {
        Toast.makeText(getBaseContext(), getString(R.string.errorFTPparameters), Toast.LENGTH_LONG).show();
        setResult(11);
        finish();
    }

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];

        // byte array transfers
        Bitmap imgTaken = BitmapFactory.decodeFile(from, options);

        if (imgTaken == null) {
            Toast.makeText(getBaseContext(), getString(R.string.errorcreatingstream), Toast.LENGTH_LONG).show();

            setResult(11);
            finish();
        }

        // transforming image into byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        imgTaken.compress(CompressFormat.JPEG, 60, bos);
        byte[] bitmapdata = bos.toByteArray();

        // base64 encoding of the bytes in a string
        String bos1 = Base64.encodeBytes(bitmapdata);
        //...
Agriesean
  • 155
  • 1
  • 16

1 Answers1

0

Well, your image is to big to fit in the memory, that Android has allocated for you. If you don't need the full resolution picture, try setting the image size upfront to something smaller using inSampleSize:

options.inSampleSize = 2 //gives you an image with half the height/width

If you need the full resolution picture, please clarify what exactly you want to achieve with your transferImage function, then maybe we can provide you with an alternative way.

Jan Gerlinger
  • 7,361
  • 1
  • 44
  • 52
  • i Have test but it's not the problem's. It's because the totality of the memory allowed it's insufficient. How to change the allowed memory? – Agriesean Jul 19 '12 at 07:49
  • 1
    Android decides how much memory you get, you can't influence it. Try some higher values for `inSampleSize` or use some approach to [automatically calculate it](http://stackoverflow.com/a/823966/1467115). – Jan Gerlinger Jul 19 '12 at 08:08