一段时间后,Gridview缩略图消失了

时间:2012-03-27 11:56:23

标签: android gridview thumbnails visible

我有一个关于我正在制作课程的图库/图片标签应用的问题。

我的应用是一个图库/标签图片应用。首先显示一个gridview,显示存储在SD卡上的所有手机图片的缩略图。 我遇到的问题是在这个行动之后: Gridview - > 用户点击图片 - > Fullview of picture - > 用户点击回复 - >回到gridview 因此,在返回到gridview后,当滚动时,不会直接查看缩略图块,但是对象仍然存在且可点击但缩略图不可见。

希望得到一些帮助: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 所以这就是bug发生前的视图。所有的图片都在那里,然后在从fullview返回并稍后滚动之后,错误发生并且视图看起来像下面的那个。注意:它会保留所有可见的图片,以及缩略图消失的对象,它们仍然可以点击。 After bug picture

当尝试获取应该返回视图的其中一个图像时,Logcat会这样说:

  

无法打开内容:content:// media / external / images / thumbnails / 0   java.io.FileNotFoundException:没有内容条目:// media / external / images / thumbnails / 0   然后是一些额外的东西,最后:   resolveUri在错误的位图uri上进行了分析:content:// media / external / images / thumbnails / 0

所以不知何故,图像适配器类无法找到应该返回当前视图的图片的任何缩略图。 我会包含整个logcat条目的图像,但我目前不允许发布超过2个链接:)

希望这是您需要的所有信息:) 我会很高兴能得到任何帮助!

2 个答案:

答案 0 :(得分:1)

我自己发现了这个问题。问题在于我使用相同的变量获取缩略图然后获取全景图片,所以当我回到图库屏幕时,缩略图查询得到了错误的表格。实质上,它试图从完整图片的表格中获取缩略图。

学到的东西,无缘无故地重用全局变量=坏:D

答案 1 :(得分:0)

试试这个:

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);
}