我正在开发一个在GridView中显示WhatsApp状态图像的应用程序。启动应用程序时,它可以正确显示图像,但显示一些空白的网格。由于目录中有视频文件,所以我知道此空白的网格项,但是我不知道如何将图像文件与视频分开。我使用位图在网格上显示图像。因此它显示空白的gridview。我只需要在gridview上的图像。
这是我的MainActivity
public class MainActivity extends AppCompatActivity {
private String[] FilePathStrings;
private String[] FileNameStrings;
private File[] listFile;
GridView grid;
GridViewAdapter adapter;
File file;
String extension = ".jpg";
String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Locate the image folder in your SD Card
file = new File("/storage/emulated/0/WhatsApp/Media/.Statuses/");
if (file.isDirectory()) {
listFile = file.listFiles();
// Create a String array for FilePathStrings
FilePathStrings = new String[listFile.length];
// Create a String array for FileNameStrings
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
if(listFile[i].getName().endsWith(extension)) {
// Get the path of the image file
FilePathStrings[i] = listFile[i].getAbsolutePath();
// Get the name image file
FileNameStrings[i] = listFile[i].getName();
}
}
}
// Locate the GridView in gridview_main.xml
grid = (GridView) findViewById(R.id.gridview);
grid.setColumnWidth(200);
grid.setHorizontalSpacing(10);
grid.setVerticalSpacing(10);
grid.setNumColumns(2);
grid.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
// Pass String arrays to LazyAdapter Class
adapter = new GridViewAdapter(this,FilePathStrings, FileNameStrings);
// Set the LazyAdapter to the GridView
grid.setAdapter(adapter);
这是我的GridViewAdapter
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewAdapter extends BaseAdapter{
private Activity activity;
private String[] filepath;
private String[] filename;
private static LayoutInflater inflater = null;
public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
activity = a;
filepath = fpath;
filename = fname;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
// Locate the TextView in gridview_item.xml
TextView text = (TextView) vi.findViewById(R.id.text);
// Locate the ImageView in gridview_item.xml
ImageView image = (ImageView) vi.findViewById(R.id.image);
// Set file name to the TextView followed by the position
text.setText(filename[position]);
// Decode the filepath with BitmapFactory followed by the position
Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
// Set the decoded bitmap into ImageView
image.setImageBitmap(bmp);
return vi;
}
}
答案 0 :(得分:0)
我已经通过调试检查了您的代码,并且可以正常工作,但是您可以显示GridViewAdapter吗?
答案 1 :(得分:0)
Note
我已经使用recyclerview and RecyclerView adapter
实现了此功能。
无需在单独的项目位置插入视频文件。
您的ItemHolder
应该有两个ImageViews
。一个用于项,另一个用于Video Icon
。最初Video Icon ImageView
的可见度为Gone or Invisible
。在适配器中绑定数据时,只需检查文件路径即可。如果文件的路径以视频扩展名结尾,则Video Icon ImageView
的可见性应更改为“可见”。我在这里添加XML and item Holder's code
。
Recyleriew
项目
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv_status_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/colorWhite">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.github.siyamed.shapeimageview.RoundedImageView
android:id="@+id/iv_status"
android:layout_width="match_parent"
android:layout_height="90dp"
app:siRadius="6dp"
app:siBorderColor="#dfdfdf"
app:siBorderWidth="1dp"
app:siSquare="true"
/>
<ImageView
android:id="@+id/iv_play_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:contentDescription="@null"
android:visibility="gone"
android:background="@drawable/toolbar_transparent"
app:srcCompat="@drawable/float_video_play" />
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/cb_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
从ItemHolder
到BindData
的代码
public void bindData(StatusObject statusObject, int position, int size, boolean selectionMode) {
super.bindData(statusObject, position, size);
this.selectionMode = selectionMode;
if (statusObject.getPath().endsWith(".mp4")) {
ivPlayIcon.setVisibility(View.VISIBLE);
}
else if (statusObject.getPath().endsWith(".gif")){
ivPlayIcon.setVisibility(View.VISIBLE);
}
else ivPlayIcon.setVisibility(View.GONE);
if (selectionMode)
cbSelection.setVisibility(View.VISIBLE);
else cbSelection.setVisibility(View.GONE);
cbSelection.setChecked(statusObject.isSelected());
cbSelection.setClickable(false);
// ivStatus.setClipToOutline(true);
Glide.with(itemView)
.asBitmap()
.load(Uri.fromFile(new File(statusObject.getPath())))
.into(ivStatus);
}