Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:34:38+00:00 2026-05-30T08:34:38+00:00

My app is a Wifi chat app with which you can communicate between two

  • 0

My app is a Wifi chat app with which you can communicate between two Android units with text messages and snap camera pictures and send them. The pictures are stored to the SD-card.

I used to have an OutOfMemoryError thrown after a couple of sent images, but I solved that problem by sending the

options.inPurgeable = true;

and

options.inInputShareable = true;

to the BitmapFactory.decodeByteArray method. This makes the pixels “deallocatable” so new images can use the memory. Thus, the error no longer remains.

But, the internal memory is still full of images and the “Low on space: Phone storage space is getting low” warning appears. The app no longer crashes but there’s no more memory on the phone after the app finishes. I have to manually clear the app’s data in Settings > Applications > Manage Applications.

I tried recycling the bitmaps and even tried to explicitly empty the app’s cache, but it doesn’t seem to do what i expect.

This function receives the picture via a TCP socket, writes it to the SD-card and starts my custom Activity PictureView:

public void receivePicture(String fileName) {
    try {
        int fileSize = inStream.readInt();
        Log.d("","fileSize:"+fileSize);
        byte[] tempArray = new byte[200];
        byte[] pictureByteArray = new byte[fileSize];

        path = Prefs.getPath(this) + "/" + fileName;
        File pictureFile = new File(path);
        try {
          if( !pictureFile.exists() ) {
                pictureFile.getParentFile().mkdirs();
                pictureFile.createNewFile();
            }
        } catch (IOException e) { Log.d("", "Recievepic - Kunde inte skapa fil.", e); }

        int lastRead = 0, totalRead = 0;
        while(lastRead != -1) {
            if(totalRead >= fileSize - 200) {
                lastRead = inStream.read(tempArray, 0, fileSize - totalRead);
                System.arraycopy(tempArray, 0, pictureByteArray, totalRead, lastRead);
                totalRead += lastRead;
                break;
            }
            lastRead = inStream.read(tempArray);
            System.arraycopy(tempArray, 0, pictureByteArray, totalRead, lastRead);
            totalRead += lastRead;
        }

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pictureFile));
        bos.write(pictureByteArray, 0, totalRead);
        bos.flush();
        bos.close();

        bos = null;
        tempArray = null;
        pictureByteArray = null;

        setSentence("<"+fileName+">", READER);
        Log.d("","path:"+path);
        try {
            startActivity(new Intent(this, PictureView.class).putExtra("path", path));
        } catch(Exception e) { e.printStackTrace(); }
    }
    catch(IOException e) { Log.d("","IOException:"+e); }
    catch(Exception e) { Log.d("","Exception:"+e); }
}

Here’s PictureView. It creates a byte[ ] from the file on the SD-card, decodes the array to a Bitmap, compresses the Bitmap and writes it back to the SD-card. Lastly, in the Progress.onDismiss, the picture is set as the image of a full screen imageView:

public class PictureView extends Activity {

private String fileName;
private ProgressDialog progress;

public ImageView view;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    Log.d("","onCreate() PictureView");

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    view = new ImageView(this);
    setContentView(view);

    progress = ProgressDialog.show(this, "", "Laddar bild...");
    progress.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
            File file_ = getFileStreamPath(fileName);
            Log.d("","SETIMAGE");
            Uri uri = Uri.parse(file_.toString());
            view.setImageURI(uri);
        }
    });

    new Thread() { public void run() {
        String path = getIntent().getStringExtra("path");
        Log.d("","path:"+path);
        File pictureFile = new File(path);

        if(!pictureFile.exists())
            finish();

        fileName = path.substring(path.lastIndexOf('/') + 1);
        Log.d("","fileName:"+fileName);

        byte[] pictureArray = new byte[(int)pictureFile.length()];

        try {
            DataInputStream dis = new DataInputStream( new BufferedInputStream(
                    new FileInputStream(pictureFile)) );
            for(int i=0; i < pictureArray.length; i++)
                pictureArray[i] = dis.readByte();
        } catch(Exception e) { Log.d("",""+e); e.printStackTrace(); }

        /**
         * Passing these options to decodeByteArray makes the pixels deallocatable
         * if the memory runs out.
         */
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inInputShareable = true;

        Bitmap pictureBM =
                BitmapFactory.decodeByteArray(pictureArray, 0, pictureArray.length, options);

        OutputStream out = null;
        try {
            out = openFileOutput(fileName, MODE_PRIVATE);

                    /**
                      * COMPRESS !!!!!
                    **/
            pictureBM.compress(CompressFormat.PNG, 100, out);

            pictureBM = null;
            progress.dismiss(); }
        catch (IOException e) { Log.e("test", "Failed to write bitmap", e); }
        finally {
            if (out != null)
                try { out.close(); out = null; }
                catch (IOException e) { }
        } }
    }.start();
}

    @Override
    protected void onStop() {
            super.onStop();
            Log.d("","ONSTOP()");
            Drawable oldDrawable = view.getDrawable();
            if( oldDrawable != null) {
                    ((BitmapDrawable)oldDrawable).getBitmap().recycle();
                    oldDrawable = null;
                    Log.d("","recycle");
            }

            Editor editor = 
                    this.getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();
    }
}

When the user presses the back key, the picture isn’t supposed to be available anymore from within the app. Just stored on the SD-card.

In onStop() I recycle the old Bitmap and even try to empty the app’s data. Still the “Low on space” warning appears. How can I be sure the images won’t allocate the memory anymore when they’re not needed?

EDIT: It appears the problem is the compress method. If everything after compress is commented, the problem remains. If I delete compress, the problem disappears. Compress seems to allocate memory that’s never released, and it’s 2-3 MB per image.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:34:39+00:00Added an answer on May 30, 2026 at 8:34 am

    Ok, I solved it. The problem was, I was passing an OutputStream to compress, which is a stream to a private file in the app’s internal memory. That’s what I set as the image later. This file is never allocated.

    I didn’t get that I had two files: one on the SD-card and one in the internal memory, both with the same name.

    Now, I’m just setting the SD-card file as the ImageView’s image. I never read the file into the internal memory as a byte[], thus never decoding the array to a bitmap, thus never compressing the bitmap into the internal memory.

    This is the new PictureView:

    public class PictureView extends Activity {
    
        public ImageView view;
        private String path;
    
        @Override
        public void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            Log.d("","onCreate() PictureView");
    
            path = getIntent().getStringExtra("path");
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            view = new ImageView(this);
            setContentView(view);
    
            Uri uri = Uri.parse( new File(path).toString() );
            view.setImageURI(uri);
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                Log.d("","Back key pressed");
    
                Drawable oldDrawable = view.getDrawable();
                    if( oldDrawable != null) {
                        ((BitmapDrawable)oldDrawable).getBitmap().recycle();
                        oldDrawable = null;
                        Log.d("","recycle");
                    }
                    view = null;
            }
            return super.onKeyDown(keyCode, event);
        }
    }
    

    Is it bad practice to put an external file as the image of an ImageView? Should I load it into internal memory first?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can my Android app find the MAC address of the Wifi access point it
I've two android applications on same LAN provided by WIFI : App (A) that
I want to code a Android app, which will connect to a WIFI printer
I am creating an iPad app, which has to communicate with a WiFi enable
I am building an Android app that uses wifi. I have properly declared these
I want to create android app that while i am in my wifi area
Possible Duplicate: Ad Hoc Wifi Connection Between iPhone & Mac - Possible? How can
what should I do in my app to support sending data between two iphones
How can wifi be turned off/on programmatically and do is rooted or system app
I'm trying to program an app to discover wifi devices which are connecting to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.