• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps GridView to show image gallery from SQLite DB

0belix

Lurker
Hi,

I've been trying to code a GridView to show all the image thumbnails that are found on a particular table on an SQLite DB.

I know the DB is being read, and the issue seems to be binding the images to the GridView object. Here's the code i'm using;

Code:
public class InputItemDataActivity extends AppCompatActivity {

    static final int CAM_REQUEST = 1;
    String baseFolderName = "MyApp";
    String itemId = "";
    String imageFileName = "";
    String[] mThumbIds;
    ArrayList<String> allImages = new ArrayList<String>();

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_item_data);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        itemId = intent.getStringExtra(AddItemActivity.EXTRA_MESSAGE);
        TextView textView = (TextView) findViewById(R.id.itemId);
        textView.setText(itemId);

        LoadImages(itemId);

    }

    public class ImageAdapter extends BaseAdapter{
        private Context mContext;

        public ImageAdapter(Context c){
            mContext = c;
        }

        public int getCount(){
            //return mThumbIds.length;
            return allImages.size();
        }

        public Object getItem(int position){
            return null;
        }

        public long getItemId(int position){
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent){
            ImageView imageView;
            if (convertView == null){
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85,85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);
            }else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageDrawable(Drawable.createFromPath(allImages.get(position)));

            return imageView;

        }

    }

    public void cancelAction(View view){

        Intent intent = new Intent(this, AddItemActivity.class);
        startActivity(intent);

    }

    public void captureImage(View view){

        Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = getFile();
        camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(camera_intent, CAM_REQUEST);

    }

    private File getFile(){

        File folder = new File("sdcard/" + baseFolderName);

        if(!folder.exists()){
            folder.mkdir();
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imageFileName = itemId + "_" + timeStamp + ".jpg";

        File image_file = new File(folder, imageFileName);

        return image_file;

    }

    private void InsertImageIntoDB(String itemId, String imageFilename){

        SQLiteDatabase myDB = null;
        String ItemTableName = "Image";
        String erro = "";
        String Data="";

        try {

            // Declara a DB a usar
            myDB = this.openOrCreateDatabase("myDB", MODE_PRIVATE, null);

            myDB.execSQL("INSERT INTO " + ItemTableName + " (QRCodeId, Image) VALUES ('" + itemId + "', '" + imageFilename + "');");
        } catch(Exception e) {
            Log.e("Error", "Error", e);
            erro = e.getMessage();
            TextView errorPanel = (TextView) findViewById(R.id.errorPanel);
            errorPanel.setText("Insert Image Error: " + erro);
            errorPanel.setVisibility(View.VISIBLE);
        } finally {
            if (myDB != null) {
                myDB.close();
            }
        }

    }


    [USER=1021285]@override[/USER]
    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        String path="sdcard/" + baseFolderName + "/" + imageFileName;

        // Add photo to DB
        InsertImageIntoDB(itemId, imageFileName);

        LoadImages(itemId);

    }

    private void LoadImages(String itemId){

        SQLiteDatabase myDB= null;
        String erro = "";
        String allItems = "";


        try {

            myDB = this.openOrCreateDatabase("myDB", MODE_PRIVATE, null);

            Cursor c = myDB.rawQuery("SELECT Image.ItemId, Image.QRCodeId, Image.Image FROM Image;" , null);

            int itemIdInt = c.getColumnIndex("ItemId");
            int QRCodeIdInt = c.getColumnIndex("QRCodeId");
            int imageInt = c.getColumnIndex("Image");

            // Check if our result was valid.
            c.moveToFirst();
            if (c != null) {
                // Loop through all Results
                do {
                    int currentItemId = c.getInt(itemIdInt);
                    String currentQRCodeId = c.getString(QRCodeIdInt);
                    String currentImageFile = c.getString(imageInt);

                    String path="sdcard/" + baseFolderName + "/" + currentImageFile;

                    allImages.add(path);

                }while(c.moveToNext());

            }else{

                erro = "No items were found on the DB";

            }

        } catch(Exception e) {
            Log.e("Error", "Error", e);
            erro = e.getMessage();
        } finally {
            if (myDB != null) {
                myDB.close();
            }

        }

        if(erro != "") {
            TextView errorPanel = (TextView) findViewById(R.id.errorPanel);
            errorPanel.setText("Read Error: " + erro);
            errorPanel.setVisibility(View.VISIBLE);
        }else{

            GridView gridView = (GridView) findViewById(R.id.ImageGrid);
            gridView.setAdapter(new ImageAdapter(this));

        }

    }

}

And this is the Layout:

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_input_item_data"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="[USER=696546]@String[/USER]/appbar_scrolling_view_behavior"
    tools:context="en.company.myapp.InputItemDataActivity"
    tools:showIn="@layout/activity_input_item_data">

    <TextView android:id="@+id/errorPanel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"/>

    <TextView
        android:text="Título"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:id="@+id/title" />

    <TextView
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/itemIdLabel"
        android:layout_toRightOf="@+id/itemIdLabel"
        android:layout_toEndOf="@+id/itemIdLabel"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:id="@+id/itemId" />

    <TextView
        android:text="[USER=696546]@String[/USER]/itemIdLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:id="@+id/itemIdLabel"
        android:layout_below="@+id/title"
        android:layout_alignLeft="@+id/title"
        android:layout_alignStart="@+id/title" />

    <TextView
        android:text="[USER=696546]@String[/USER]/commentLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/itemIdLabel"
        android:layout_alignLeft="@+id/itemIdLabel"
        android:layout_alignStart="@+id/itemIdLabel"
        android:layout_marginTop="20dp"
        android:id="@+id/comment" />

    <EditText
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:layout_below="@+id/comment"
        android:layout_alignLeft="@+id/comment"
        android:layout_alignStart="@+id/comment"
        android:layout_marginTop="17dp"
        android:id="@+id/editText"
        android:layout_width="wrap_content" />

    <Button
        android:text="[USER=696546]@String[/USER]/btnCancelText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:eek:nClick="cancelAction"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="[USER=696546]@String[/USER]/btnCaptureImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:eek:nClick="captureImage"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="[USER=696546]@String[/USER]/btnSaveToDB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <GridView
        android:layout_width="match_parent"
        android:layout_below="@+id/editText"
        android:layout_marginTop="23dp"
        android:layout_height="220dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/ImageGrid"/>

</RelativeLayout>
 
Last edited by a moderator:
The activity is a follow up of a previous one where i capture an id from a barcode that gets scanned by the camera. That bit works fine, and also the image capturing that gets associated with taht id and stored on the DB.
The DB also returns the proper images and the issue seems to be on the binding of the GridView, since whenever i comment it, it works fine, just no images on the GridView, if i uncomment it, it crashes... help please!!!!
 
If it crashes, please attach the stack trace from your Logcat output. Thanks

btw I added some [code][/code] tags to your code to make it readable.
 
If it crashes, please attach the stack trace from your Logcat output. Thanks

btw I added some [code][/code] tags to your code to make it readable.

Hi LV426,

Here's the stack trace from the LogCat:

Code:
[2017-03-03 16:15:41 - ddms] Can't bind to local 8600 for debugger


[2017-03-03 16:15:41 - ddms] Can't bind to local 8600 for debugger

[2017-03-03 16:15:41 - ddmlib] An established connection was aborted by the software in your host machine

java.io.IOException: An established connection was aborted by the software in your host machine

at sun.nio.ch.SocketDispatcher.write0(Native Method)

at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)

at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)

at sun.nio.ch.IOUtil.write(IOUtil.java:65)

at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)

at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)

at com.android.ddmlib.Client.sendAndConsume(Client.java:684)

at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)

at com.android.ddmlib.Client.requestAllocationStatus(Client.java:523)

at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:564)

at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:539)

at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:501)

at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:397)

at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:64)

at com.android.ddmlib.DeviceMonitor$1.run(DeviceMonitor.java:320)


It does not seem to be able to debug, although the Debugger Options are on.

This is the EventLog from Android Studio:

Code:
16:19:07 Executing tasks: [:app:assembleDebug]
16:19:08 Gradle build finished in 939ms
16:19:09 Can't bind to local 8600 for debugger
16:19:10 Can't bind to local 8600 for debugger
16:19:10 Can't bind to local 8600 for debugger
 
Last edited:
Let's rephrase the question. You said:

The DB also returns the proper images and the issue seems to be on the binding of the GridView, since whenever i comment it, it works fine, just no images on the GridView, if i uncomment it, it crashes... help please!!!!

So my questions to you are:-

1. What code did you uncomment to cause the application crash?
2. What is the stack trace in the Logcat when this crash happens?

Also, please put any code, and system output like stack trace in code tags, like this:


[code]
some code here
[/code]
 
Let's rephrase the question. You said:



So my questions to you are:-

1. What code did you uncomment to cause the application crash?
2. What is the stack trace in the Logcat when this crash happens?

Also, please put any code, and system output like stack trace in code tags, like this:


[code]
some code here
[/code]

Hi,

The line that when uncommented causes the crash is line 194 of the first code piece i posted.

No stacktrace comes out of the crash, just a message on the device (Galaxy S3) stating "unfortunately myApp has stopped." and nothing more. No errors on the Android Studio are reported and it seems to be unable to debug the device via the LogCat as i explained on the previous reply where i posted both Stack Traces.
 

This is the Stack Trace i get from the ADM after solving the debugger bind issue

Code:
[2017-03-03 18:04:21 - ddmlib] An established connection was aborted by the software in your host machine

java.io.IOException: An established connection was aborted by the software in your host machine

at sun.nio.ch.SocketDispatcher.write0(Native Method)

at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)

at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)

at sun.nio.ch.IOUtil.write(IOUtil.java:65)

at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)

at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)

at com.android.ddmlib.Client.sendAndConsume(Client.java:684)

at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)

at com.android.ddmlib.Client.requestAllocationStatus(Client.java:523)

at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:564)

at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:539)

at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:501)

at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:397)

at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:64)

at com.android.ddmlib.DeviceMonitor$1.run(DeviceMonitor.java:320)



[2017-03-03 18:04:26 - ddmlib] An established connection was aborted by the software in your host machine

java.io.IOException: An established connection was aborted by the software in your host machine

at sun.nio.ch.SocketDispatcher.write0(Native Method)

at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)

at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)

at sun.nio.ch.IOUtil.write(IOUtil.java:65)

at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)

at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)

at com.android.ddmlib.Client.sendAndConsume(Client.java:684)

at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)

at com.android.ddmlib.Client.requestAllocationStatus(Client.java:523)

at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:564)

at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:539)

at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:501)

at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:397)

at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:64)

at com.android.ddmlib.DeviceMonitor$1.run(DeviceMonitor.java:320)
 
Code:
01-16 02:40:21.996: E/AndroidRuntime(20546): FATAL EXCEPTION: main
01-16 02:40:21.996: E/AndroidRuntime(20546): java.lang.OutOfMemoryError
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.graphics.drawable.Drawable.createFromPath(Drawable.java:952)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at pt.useit.conservationapp.InputItemDataActivity$ImageAdapter.getView(InputItemDataActivity.java:89)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.AbsListView.obtainView(AbsListView.java:2608)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.GridView.onMeasure(GridView.java:1045)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1375)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.View.measure(View.java:16831)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.Choreographer.doCallbacks(Choreographer.java:591)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.Choreographer.doFrame(Choreographer.java:561)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.os.Handler.handleCallback(Handler.java:730)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.os.Looper.loop(Looper.java:176)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at android.app.ActivityThread.main(ActivityThread.java:5419)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at java.lang.reflect.Method.invokeNative(Native Method)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at java.lang.reflect.Method.invoke(Method.java:525)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-16 02:40:21.996: E/AndroidRuntime(20546):  at dalvik.system.NativeStart.main(Native Method)
 
Thx!!! By refering to that link i was able to work out a solution using these two functions:

Code:
public static Bitmap decodeFile(String pathName) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
        try {
            bitmap = BitmapFactory.decodeFile(pathName, options);
            Log.d("No Error", "Decoded successfully for sampleSize " + options.inSampleSize);
            break;
        } catch (OutOfMemoryError outOfMemoryError) {
            // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
            Log.e("Error", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
                    + " retrying with higher value");
        }
    }
    return bitmap;
}

private static Bitmap getResizedBitmap(Bitmap bitmap, float maxWidth, float maxHeight) {

    float width = bitmap.getWidth();
    float height = bitmap.getHeight();
    if (width > maxWidth) {
        height = (maxWidth / width) * height;
        width = maxWidth;
    }
    if (height > maxHeight) {
        width = (maxHeight / height) * width;
        height = maxHeight;
    }
    return Bitmap.createScaledBitmap(bitmap, (int) width, (int) height, true);

}

And using

Code:
Bitmap bitmap = decodeFile(allImages.get(position));

imageView.setImageBitmap(bitmap);

Bitmap bitmapThumb = getResizedBitmap(bitmap, 85, 85);

imageView.setImageBitmap(bitmapThumb);

instead of

Code:
imageView.setImageDrawable(Drawable.createFromPath(allImages.get(position)));
 
Back
Top Bottom