我试图实现如下图所示的事情。这里当recyclerview为空时,它将在页脚中显示摄像机图像视图,当我点击图像时,我可以从摄像机或图库中选择图像(将保存在内部存储文件夹中)并在recyclerview中显示它们。但我在ArrayIndexOutOfBoundsException
的onBindViewHolder中获得case 1
。如何显示内部文件夹中的图像?
java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
at java.util.ArrayList.get(ArrayList.java:413)
at com.sam.testapp.ImageAdapter.onBindViewHolder(ImageAdapter.java:85)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)

// Activity
public class MainActivity extends AppCompatActivity{
Toolbar toolbar;
RecyclerView recycler;
ImageAdapter adapter;
private ArrayList<Object> itemList;
String[] items = new String [] {"http://i.imgur.com/7P2v384.jpg", "http://i.imgur.com/Fb95GwC.jpg", "http://i.imgur.com/qciJxBg.jpg","http://i.imgur.com/h2jgdAd.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recycler = (RecyclerView)findViewById(R.id.recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, OrientationHelper.HORIZONTAL, false);
recycler.setLayoutManager(linearLayoutManager);
recycler.setItemAnimator(new DefaultItemAnimator());
adapter = new ImageAdapter(MainActivity.this, itemList);
recycler.setAdapter(adapter);
//Collections.addAll(itemList, items);
itemList = new ArrayList<Object>(Arrays.asList(items));
itemList.add(1);
}
}
&#13;
// Adapter
public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private static final int TYPE_FOOTER = 0;
private static final int TYPE_ITEM = 1;
final CharSequence[] items = {
"Camera", "Gallery", "Cancel"
};
private ArrayList<Object> finalImages = new ArrayList<>();
private static final String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TESTAPP";
public ImageAdapter(Context context, ArrayList<Object> itemList) {
this.finalImages = itemList;
this.mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 0:
View v0 = LayoutInflater.from(parent.getContext()).inflate(R.layout.customrowlayout, parent, false);
viewHolder = new VHFooter(v0);
break;
case 1:
View v1 = inflater.inflate(R.layout.imagerowlayout, parent, false);
viewHolder = new VHItem(v1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 0:
VHFooter vh0 = (VHFooter) holder;
vh0.imgv.setImageResource(R.drawable.camera);
vh0.imgv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
break;
case 1:
VHItem vh1 = (VHItem) holder;
String path = (String) finalImages.get(position);
if (!TextUtils.isEmpty(path)) {
Picasso.with(mContext).load(path).into(vh1.image);
}
break;
}
}
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) mContext).startActivityForResult(intent, 0);
dialog.dismiss();
} else if (item == 1) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity) mContext).startActivityForResult(intent, 1);
dialog.dismiss();
} else if (item == 2) {
dialog.dismiss();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 0) {
savefile(data, true);
loadfile();
} else if (resultCode == RESULT_OK && requestCode == 1) {
savefile(data, false);
loadfile();
}
}
private void savefile(Intent data, boolean isCamera) {
if (isCamera) {
Bitmap bmap = (Bitmap) data.getExtras().get("data");
File directory = new File(file_path);
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directory, "myimage.jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = mContext.getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmap = (BitmapFactory.decodeFile(picturePath));
File directory = new File(file_path);
if (!directory.exists()) {
directory.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.ENGLISH).format(new Date());
File file = new File(directory, "IMG_" + timeStamp + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void loadfile() {
File directory = new File(file_path);
File newfile[] = directory.listFiles();
Log.e("sammy_arrlistsize_befor", " " + finalImages.size());
if (finalImages != null) finalImages.clear();
for (int i = 0; i < newfile.length; i++) {
String path = "file://" + newfile[i].getAbsolutePath();
finalImages.add(path);
Log.e("sammy_imagepath", " " + path);
}
Log.e("sammy_arrlistsize_after", " " + finalImages.size());
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return this.finalImages.size();
}
@Override
public int getItemViewType(int position) {
if (finalImages.get(position) instanceof String) {
return TYPE_ITEM;
} else if (finalImages.get(position) instanceof Integer) {
return TYPE_FOOTER;
}
return -1;
}
class VHItem extends RecyclerView.ViewHolder {
ImageView image;
public VHItem(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
class VHFooter extends RecyclerView.ViewHolder {
ImageView imgv;
public VHFooter(View itemView) {
super(itemView);
imgv = (ImageView) itemView.findViewById(R.id.imgv);
}
}
}
&#13;
答案 0 :(得分:1)
您的异常被抛出,因为
@Override
public int getItemCount() {
return this.finalImages.size()+1;
}
应该是
@Override
public int getItemCount() {
return this.finalImages.size();
}
答案 1 :(得分:1)
MainActivity中的第一个
private ArrayList<Object> itemList = new ArrayList<>();
itemList.addAll(yourStringList);
itemList.add(1);
然后使用下面的适配器类
public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private static final int TYPE_FOOTER = 0;
private static final int TYPE_ITEM = 1;
final CharSequence[] items = {
"Camera", "Gallery", "Cancel"
};
private ArrayList<Object> finalImages = new ArrayList<>();
private static final String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TESTAPP";
public ImageAdapter(Context context, ArrayList<Object> itemList) {
this.finalImages = itemList;
this.mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 0:
View v0 = LayoutInflater.from(parent.getContext()).inflate(R.layout.customrowlayout, parent, false);
viewHolder = new VHFooter(v0);
break;
case 1:
View v1 = inflater.inflate(R.layout.imagerowlayout, parent, false);
viewHolder = new VHItem(v1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 0:
VHFooter vh0 = (VHFooter) holder;
vh0.imgv.setImageResource(R.drawable.camera);
// Picasso.with(mContext).load(R.drawable.camera).into(vh0.imgv);
//piccaso is commented for now
vh0.imgv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
break;
case 1:
VHItem vh1 = (VHItem) holder;
String path = (String) finalImages.get(position);
if (!TextUtils.isEmpty(path)) {
Picasso.with(mContext).load(path).into(vh1.image);
}
break;
}
}
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) mContext).startActivityForResult(intent, 0);
dialog.dismiss();
} else if (item == 1) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity) mContext).startActivityForResult(intent, 1);
dialog.dismiss();
} else if (item == 2) {
dialog.dismiss();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 0) {
savefile(data, true);
loadfile();
} else if (resultCode == RESULT_OK && requestCode == 1) {
savefile(data, false);
loadfile();
}
}
private void savefile(Intent data, boolean isCamera) {
if (isCamera) {
Bitmap bmap = (Bitmap) data.getExtras().get("data");
File directory = new File(file_path);
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directory, "myimage.jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = mContext.getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmap = (BitmapFactory.decodeFile(picturePath));
File directory = new File(file_path);
if (!directory.exists()) {
directory.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.ENGLISH).format(new Date());
File file = new File(directory, "IMG_" + timeStamp + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void loadfile() {
File directory = new File(file_path);
File newfile[] = directory.listFiles();
Log.e("sammy_arrlistsize_befor", " " + finalImages.size());
if (finalImages != null) finalImages.clear();
for (int i = 0; i < newfile.length; i++) {
String path = "file://" + newfile[i].getAbsolutePath();
finalImages.add(path);
Log.e("sammy_imagepath", " " + path);
}
Log.e("sammy_arrlistsize_after", " " + finalImages.size());
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return this.finalImages.size();
}
@Override
public int getItemViewType(int position) {
if (finalImages.get(position) instanceof String) {
return TYPE_ITEM;
} else if (finalImages.get(position) instanceof Integer) {
return TYPE_FOOTER;
}
return -1;
}
class VHItem extends RecyclerView.ViewHolder {
ImageView image;
public VHItem(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
class VHFooter extends RecyclerView.ViewHolder {
ImageView imgv;
public VHFooter(View itemView) {
super(itemView);
imgv = (ImageView) itemView.findViewById(R.id.imgv);
}
}
}