gpt4 book ai didi

android - Gridview 缩略图在一段时间后消失

转载 作者:行者123 更新时间:2023-11-30 04:16:38 25 4
gpt4 key购买 nike

我有一个关于我为类(class)制作的画廊/图片标签应用程序的问题。

我的应用程序是一个图库/标签图片应用程序。它首先显示一个 GridView ,其中显示存储在 SD 卡上的所有手机图片的缩略图。我遇到的问题是在执行此操作之后:Gridview -> 用户点击图片 -> 图片全 View -> 用户点击返回 -> 返回到 gridview所以在返回到 gridview 后,当四处滚动时,不在直接 View 中的缩略图 block 将被删除,但是对象仍然存在并且可以单击但缩略图不可见。

希望得到一些帮助: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所以这是错误发生之前 View 的样子。所有图片都在那里,然后从全 View 返回并稍后滚动后,出现错误, View 如下所示。注意:它保留了所有一直可见的图片,以及缩略图消失的对象,它们仍然可以点击。 After bug picture

Logcat 在尝试取回应该返回到 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

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

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

最佳答案

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

经验教训,无缘无故地重用全局变量 = 不好 :D

关于android - Gridview 缩略图在一段时间后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9889092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com