java.lang.Outofmemoryerror: Failed to allocate error

Asked

Viewed 635 times

1

This error in my application is happening:

java.lang.Outofmemoryerror: Failed to allocate

my image code:

URL url1 = null;
    try {
        url1 = new URL("http://www.cm-mgrande.pt" + cabecalho);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }


    try {
        InputStream input = url1.openConnection().getInputStream();
        Bitmap bmp = BitmapFactory.decodeStream(input);
        input.close();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(url1.openStream()));
        in.close();
        imgcabecalho.setImageBitmap(bmp);
        imgcabecalho.setScaleType(ImageView.ScaleType.FIT_XY);
    } catch (IOException e) {
        e.printStackTrace();


    }

my Android Manifest :

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:largeHeap="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <activity
        android:name=".Index"
    android:configChanges="orientation|screenSize"
        android:largeHeap="true"
        android:label="@string/app_name" >



    </activity>
</application>

  • 1

    What’s that for BufferedReader?

  • A good question but at the time I needed to run the code

  • In the code you posted it is doing nothing. The error may be that there is not enough memory to allocate the BufferedReader. Delete it and try again. If it is not enough see this reply in Soen. It has nothing to do with the error but it will be necessary to include in Manifest.xml access to the internet

  • error copying I am using <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  • I removed the bufferedreader and nothing worked yet

  • Is the error the same or another? By forgetfulness I did not put the link to the reply that I mentioned in the other comment.

  • is the same now tried so Bitmap bmp = Bitmapfactory.decodeStream(input,options); but error in options

  • Bitmapfactory.Options options=new Bitmapfactory.Options(); options.inSampleSize=2; Inputstream input = url1.openConnection(). getInputStream(); Bitmap bmp = Bitmapfactory.decodeStream(input,options); input.close();

Show 3 more comments

1 answer

1


Bitmap is probably too big for the memory allocated by your app. The documentation recommends several precautions when dealing with the creation of Bitmaps, especially in function of memory. Here is enough useful information.

I recommend that you create a bitmap according to the size that will be displayed. For this you need to first mark the bitmap as inJustDecodeBounds, so you’ll only take his information to make a scale reduction and not load it into memory.

    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath , options );

    // faça o cálculo da sua escala
    int imageWidth = options.outWidth;
    scale   = Math.round( imageHeight / width );
    options.inSampleSize    = scale;

    //Remove marcação e inicia o trabalho no bitmap
    options.inJustDecodeBounds  = false;

    // Cria o bitmap com o tamanho reduzido
    Bitmap scaledBitmap = BitmapFactory.decodeFile(filePath , options);

This operation is expensive and I don’t recommend doing it in the UI. Create an Asynctask or something. A tip is that there are several very good librarys that do this service. A Universal Loader works great.

I hope I’ve helped.

  • Totally right, it helped a lot! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.