我有一個畫廊/圖片標籤應用程序即時製作課程的問題。Gridview縮略圖在一段時間後消失
我的應用程序是一個畫廊/標籤圖片應用程序。它首先顯示一個gridview,顯示存儲在SD卡上的所有手機圖片的縮略圖。 問題IM經歷是這個行動後: 的GridView - > Fullview圖片 - - 在PIC>用戶點擊>用戶點擊回 - >返回所以已經返回到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 所以這是視圖看起來像發生錯誤之前。所有圖片都在那裏,然後在從fullview和稍後滾動返回後,發生錯誤並且視圖看起來像下面的圖片。注意:它保留了整個時間內所有可見的圖片,以及縮略圖消失的對象,它們仍然是可點擊的。 After bug picture
logcat的說,這個試圖接回來是應該返回查看圖像之一時:
無法打開內容:內容://媒體/外部/圖像/縮略圖/ 0 java.io.FileNotFoundException:沒有條目的內容:// media/external/images/thumbnails/0 然後是一些額外的東西,最後是: resolveUri fulled on bad bitmap uri:content:// media/external/images/thumbnails/0
因此,圖像適配器類無法找到任何應該返回到當前視圖的圖片的縮略圖。 我就已經包括了這整個logcat的條目的圖像,但即時通訊目前不能發佈超過2個鏈接:)
希望這是你需要:) 會很高興的任何幫助我能得到的所有信息!
你猜怎麼着,我犯了類似的錯誤......和您的文章幫助了我。將視圖設置爲2次,第二次用不存在的圖像...:P – Ixx 2012-06-21 12:39:45