问题
I am working with a ByteArray
which contains multiple images.basically when I capture image from camera that byte array converted into string array and store that string to SQLite
.
like below.
and then get this string and converted to byte then trying to display images into grid view but instead of getting capture image I am getting only imageview.
here is my code :
SharedPreferences settings = getSharedPreferences("image String", Context.MODE_PRIVATE);
String encodedImage = settings.getString("img str", "");
String s = encodedImage;
s = s.replace("[" , "");
s = s.replace("]" , "");
s = s.replace("," , "");
String[] strings = s.split(" ");
Log.d("LOG_TAG", " string structure : " + s);
Log.d("LOG_TAG", " string Array structure : " + Arrays.toString(strings ));
byte[][] decodedString = new byte[strings.length][];
for (int i = 0; i <= strings.length-1; i++) {
String string = strings[i];
decodedString[i] = string.getBytes(Charset.defaultCharset()); // you can chose charset
imageArry.add(new Contact1(decodedString));
Log.d("LOG_TAG", " EncodedString : " + decodedString);
Log.d("LOG_TAG", "Array Images : " + imageArry);
imageAdapter = new ImageAdapter(this, R.layout.gv_item, imageArry);
gvGallery.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
Log.d("LOG_TAG", "Selected Images" + imageArry.size());
}
my Adapter class:
class ImageAdapter extends ArrayAdapter<Contact1> {
Context context;
int layoutResourceId;
ArrayList<Contact1> data = new ArrayList<>();
public ImageAdapter(Context context, int layoutResourceId, ArrayList<Contact1> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public void setData(Contact1 contact)
{
data.add(contact);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder1 holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder1();
holder.imgIcon = (ImageView) row.findViewById(R.id.ivGallery);
row.setTag(holder);
} else {
holder = (ImageHolder1) row.getTag();
}
Contact picture = data.get(position);
//convert byte to bitmap take from contact class
byte[] outImage = picture._image;
if (outImage != null){
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
}
return row;
}
class ImageHolder1 {
ImageView imgIcon;
}
}
contact1 class:
public class Contact1 extends Contact {
byte[][] _image;
// Empty constructor
public Contact1()
{
}
public Contact1(byte[][] decodedString) {
this._image = decodedString;
}
public byte[][] get_image1()
{
return _image;
}
public void set_image1(byte[][] _image)
{
this._image = _image;
}
}
xml ivGallery:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/ivGallery"
android:layout_height="130dp"
android:layout_width="130dp"
android:padding="10dp"
android:src="@mipmap/ic_launcher_round"
android:scaleType="fitXY"
/>
</LinearLayout>
XML for gridview:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layout_gv"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
tools:ignore="NotSibling">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numColumns="5">
</GridView>
</RelativeLayout>
来源:https://stackoverflow.com/questions/62432402/how-to-display-multiple-byte-array-to-gridview