这是我的代码:
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import develop.sb.com.network.FileTransfer;
public class Gallery extends Activity {
LayoutInflater inflater;
public Context GalCon;
public GridView gv;
public ArrayList<File> list;
public ArrayList<String> UploadFileArray;
public Gallery()
{
GalCon = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
UploadFileArray = new ArrayList<String>();
list = imageReader(TextViewAdapter.selectedDir);
gv = (GridView) findViewById(R.id.gridView);
gv.setAdapter(new GridAdapter());
gv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
/*
[Position] index, bedeutet das entsprechende Element
*/
if(view.getPaddingLeft() > 0)
{
view.setPadding(0,0,0,0);
view.setBackgroundColor(Color.GREEN);
}
// Call the method you need to call on ImageView click event
else
{
view.setPadding(1, 1, 1, 1);
view.setBackgroundColor(Color.RED);
}
Toast.makeText(getApplicationContext(), list.get(position).toString(), Toast.LENGTH_SHORT).show();
UploadFileArray.add(list.get(position).toString());
File f = new File(list.get(position).toString());
System.out.println(f);
}
});
}
public class GridAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//This may be the memory leakage
LayoutInflater inflater = (LayoutInflater) GalCon.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.single_grid, parent, false);
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
/*Hier wird das Bild eingefügt*/
iv.setImageURI(Uri.parse(getItem(position).toString()));
/* iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.d("Image", "Clicked");
if(v.getPaddingLeft() > 0)
{
v.setPadding(0,0,0,0);
v.setBackgroundColor(Color.GREEN);
}
// Call the method you need to call on ImageView click event
else
{
v.setPadding(1, 1, 1, 1);
v.setBackgroundColor(Color.RED);
}
}
});*/
return convertView;
}
}
public static ArrayList<File> imageReader(String root)
{
ArrayList<File> a = new ArrayList<>();
File verzeichnistogetPic = new File(root);
File[] files = verzeichnistogetPic.listFiles();
for(int i = 0; i<files.length; i++)
{
if(files[i].isDirectory())
{
// a.addAll( imageReader(files[i]));
}
else
{
// if(files[i].getName().endsWith(".jpg"))
// {
a.add(files[i]);
// }
}
}
return a;
}
public void sendPictures(View view)
{
if(this.UploadFileArray.size() > 0 ) {
for (String temp : this.UploadFileArray) {
FileTransfer.getCameraFile(temp);
}
}
}}
我将从sdcard中的目录中获取所有文件,并创建一个带触摸事件的图库。但正如我所说。我得到了内存泄漏,如果图片太大(2-3Mb)而不是它会崩溃。如果你能帮助我,我会很高兴的。
答案 0 :(得分:1)
不要直接在imageview中添加图片,使用piccasso,volley或Universal Image lib等图片缓存库! 所有这些都缓存在内存中的图像,这导致应用程序的内存消耗更少......! 我个人使用Piccasso来实现这个目的,这是库URL,
http://square.github.io/picasso/
以下是它的简单教程, http://square.github.io/picasso/
编辑:您的GridView构造函数:
private LayoutInflater inflater; //This is your field variable
public GridAdapter()
{
inflater = (LayoutInflater) GalCon.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
你的getView()应该如下所示,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = inflater.inflate(R.layout.single_grid, parent, false);
}
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
/*Hier wird das Bild eingefügt*/
iv.setImageURI(Uri.parse(getItem(position).toString()));
/* iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.d("Image", "Clicked");
if(v.getPaddingLeft() > 0)
{
v.setPadding(0,0,0,0);
v.setBackgroundColor(Color.GREEN);
}
// Call the method you need to call on ImageView click event
else
{
v.setPadding(1, 1, 1, 1);
v.setBackgroundColor(Color.RED);
}
}
});*/
return convertView;
}