user1295313
user1295313

Reputation: 65

Gridview thumbnails disappear after a while

i have a question regarding an gallery/picture tag app im making for a course.

My application is a gallery/tag pic app. It starts by displaying a gridview that display the thumbnails of all the phones pictures stored on SD card. The problem im experiencing is after this course of action: Gridview -> user clicks on a pic -> Fullview of picture -> user clicks back -> return to gridview so after having returned to the gridview, when scrolling around, the blocks of thumbnails that is not in direct view gets removed, However the objects are still there and clickable but the thumbnail is not visible.

hope to get some help with this :D

public class App2Activity extends Activity {
/** Called when the activity is first created. */
static Cursor cursor;
static int columnIndex;
static Bitmap bMap;
static String imagePath;
protected static ContentResolver cr;
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String[] projection = {MediaStore.Images.Thumbnails._ID}; //Columns to return
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);//Puts selected columns in cursor object
    columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);//return columns index


    GridView imagesview = (GridView) findViewById(R.id.gridView1);
    imagesview.setAdapter(new ImageAdapter(this));
    //Method litsens for thumbnail that is clicked and then loads the full image bit map
    imagesview.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View v, int position, long id){
            String[] projection = {MediaStore.Images.Media.DATA};
            //This segment fetches selected image path
            cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
            columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            imagePath = cursor.getString(columnIndex);


            Intent intent = new Intent(v.getContext(), Fullview.class);

            FileInputStream in;
            BufferedInputStream buf;

            try {
                in = new FileInputStream(imagePath);
                buf = new BufferedInputStream(in);
                bMap = BitmapFactory.decodeStream(buf);
                startActivity(intent);//Start fullviewclass to project bitmap in fullscreen

                if (in != null) {
                    in.close();
                }
                if (buf != null) {
                    buf.close();
                }
            } catch (Exception e) {
                Log.e("Error reading file", e.toString());
            }

        }
    });

}
//Inflates menu when button is pressed
public boolean onCreateOptionsMenu(Menu menu){

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.exitmenu, menu);

    return true;
    }
//handles the exit menu button
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
        case R.id.exit:
        finish();
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

class ImageAdapter extends BaseAdapter{
private Context context;

public ImageAdapter(Context cont){
    context = cont;
}
@Override
public int getCount() {

    return App2Activity.cursor.getCount();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv;
    // this segment sets up the gridview
    if(convertView == null){
        iv = new ImageView(context);

        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setPadding(4, 4, 4, 4);
        iv.setLayoutParams(new GridView.LayoutParams(120, 120));
    }

    else {
        iv = (ImageView)convertView;
    }
    //fetches thumbnails
    App2Activity.cursor.moveToPosition(position);
    int imageID = App2Activity.cursor.getInt(App2Activity.columnIndex);
    iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
    return iv;
}

}

public class Fullview extends Activity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullview);
    ImageView iv = (ImageView)findViewById(R.id.imageView1);
    iv.setImageBitmap(App2Activity.bMap);

}
//Inflates menu when button is pressed
public boolean onCreateOptionsMenu(Menu menu){

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return true;
 }
//Handles menu options
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
        case R.id.back:
        finish();
        case R.id.Tag:

            Intent intent = new Intent(getApplicationContext(), ContactsDisplay.class);
            startActivityForResult(intent, 0);
            return true;

        case R.id.tagged:
            Intent intent2 = new Intent(getApplicationContext(), TaggedViewer.class);
            startActivity(intent2);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

//Gets the result Uri from choosing a contact after having pressed menu option "Tag" and saves pic/contact combination to internal storage
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode == RESULT_OK){
        if(requestCode == 0){   
            //This segment saves the tagged contact uri and hashes the associated imagepath name which become filename internal storage file
            Uri result = data.getData();
            String toBeStored = result.toString();
            Log.w("GOT", result.toString());

            int filehash = App2Activity.imagePath.hashCode();
            String strfilehash = String.valueOf(filehash);

            try {
                FileOutputStream fos = openFileOutput(strfilehash, MODE_PRIVATE);
                fos.write(toBeStored.getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                Log.w("FileWrite", "FileError");
            } catch (IOException e) {
                Log.w("FileWrite", "WriteError");
            }
        }
    }

Before bug picture so this is what the view looks like before the bug occurs. All pics are there, and then after returning from fullview and some scrolling later, the bug occur and view looks like the one below. Note: it keeps all pictures that have been visible the whole time, and also the objects whom thumbnail disappeared, they are still clickable. After bug picture

Logcat says this when trying to fetch back one of the images that is supposed to return to the view:

Unable to open content: content://media/external/images/thumbnails/0 java.io.FileNotFoundException: No entry for content://media/external/images/thumbnails/0 and then some additional stuff and finally: resolveUri fialed on bad bitmap uri: content://media/external/images/thumbnails/0

So somehow the image adapter class cant find any thumbnails for the pictures that is supposed to return to the current view. i would've included an image of this entire logcat entry but im currently not allowed to post more than 2 links :)

Hope that is all information you need :) will be happy for any help i can get!

Upvotes: 1

Views: 1393

Answers (2)

lokoko
lokoko

Reputation: 5803

Try this :

private String getPath(Uri uri) {
String[]  data = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(context, uri, data, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Upvotes: 0

user1295313
user1295313

Reputation: 65

I found the problem on my own. The problem lied in that i used the same variables for getting thumbnails and then for getting fullview pics, so when i went back to the gallery screen, the thumbnail query had gotten the wrong tables. In essence, it tried to fetch thumbnails from the table of the full pictures.

Lession learned, reusing global variables for no reason = bad :D

Upvotes: 1

Related Questions