Ich arbeite an einem benutzerdefinierten ListView-Element, das in einer SQL-Datenbank gespeichert ist. Ich habe versucht, die Bitmap in ein ByteArray umzuwandeln und dann in der Datenbank zu speichern, aber wenn ich die Aktivität verlasse und zurückkomme, ist sie nicht mehr da. Ich habe einige Fehler wieSQL-Abbild in ListView wird nicht angezeigt. Factory null
D/skia: --- SkImageDecoder::Factory returned null
, aber ich weiß nicht, wie es zu beheben ...
MainActivity von Listview:
public class PictureActivity extends AppCompatActivity {
public static final int REQUEST_IMAGE_CAPTURE = 1;
private Camera camera;
private FotoItemAdapter listAdapter;
private static final int MY_PERMISSION_CAMERA = 1;
private FrameLayout mainLayout;
private ArrayList<FotoItem> posts;
private ImageAdapter gridAdapter;
private ListView listView;
private CalendarDB FDB;
private String name;
public Bitmap image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initCamera();
initPostList();
initUI();
initDB();
initLayout();
refreshArrayList();
}
private void initLayout(){mainLayout = (FrameLayout) findViewById(R.id.fotowand);}
private void initCamera() {
camera = new Camera(this);
}
private void refreshArrayList() {
ArrayList tempList = FDB.getAllFotos();
posts.clear();
posts.addAll(tempList);
listAdapter.notifyDataSetChanged();
}
private void initDB() {
FDB = new CalendarDB(this);
FDB.open();
name = FDB.getUserName();
}
private void initPostList() {
posts = new ArrayList<>();
}
private void initUI() {
setContentView(R.layout.activity_foto_wand);
Point displaySize = getDisplaySize();
GridView grid = new GridView(getApplicationContext());
gridAdapter = new ImageAdapter(this, displaySize);
grid.setAdapter(gridAdapter);
listView = (ListView) findViewById(R.id.listview_foto_item);
listAdapter = new FotoItemAdapter(this,posts);
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.getItemId() == R.id.photo_add_button) {
checkPermission();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_CAMERA:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePicture();
} else {
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
//checkt ob ´die Berechtigung für die Aufnahme vorhanden sind. Wenn nicht wird ein PopUp geöffnet um diese zuzulassen
private void checkPermission() {
int permissionCheckCamera = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
int permissionCheckWriteExStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheckWriteExStorage == PackageManager.PERMISSION_DENIED || permissionCheckCamera == PackageManager.PERMISSION_DENIED) {
Log.d("photo", "permission ist nicht da ");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_CAMERA);
}
else Log.d("photo", "permission ist schon da "); takePicture();
}
private void takePicture() {
Log.d("photo", "*photo*");
camera.takePicture(REQUEST_IMAGE_CAPTURE);
}
private void processPicture(String path) {
Point imageSize = new Point(getDisplaySize().x, getDisplaySize().y);
image = camera.getScaledBitmap(path, imageSize);
gridAdapter.addImage(image);
gridAdapter.notifyDataSetChanged();
}
private Point getDisplaySize() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
processPicture(camera.getCurrentPhotoPath());
Log.d("foto",Integer.toString(requestCode)+" "+ Integer.toString(resultCode));
}
showPopupImage();
}
public void showPopupImage() {
// get a reference to the already created main layout
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.layout_popup_image, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.MATCH_PARENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
final ImageView imageView = (ImageView) popupView.findViewById(R.id.foto_image_popup);
imageView.setImageBitmap(gridAdapter.getItem(gridAdapter.getCount()-1));
final EditText editText = (EditText) popupView.findViewById(R.id.foto_edit_commentary_popup);
final Button buttonAdd = (Button) popupView.findViewById(R.id.foto_button_popup);
final String nameuser = FDB.getUserName();
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 10, stream);
byte[] byteArray = stream.toByteArray();
String nameuser = FDB.getUserName();
String namewg = FDB.getWGName();
FDB.insertFotoItem(editText.getText().toString(), byteArray, nameuser,namewg);
FotoItem fotoItem = new FotoItem(editText.getText().toString(), byteArray,nameuser,0);
posts.add(0, fotoItem);
listAdapter.notifyDataSetChanged();
popupWindow.dismiss();
listView.smoothScrollToPosition(0);
}
});
}
}
Adapter von FotoItem:
public class FotoItemAdapter extends ArrayAdapter<FotoItem> {
private Context context;
private CalendarDB SEDB;
private ArrayList<FotoItem> posts;
private ArrayList<CommentaryItem> comments;
private CommentaryAdapter commentaryAdapter;
public FotoItemAdapter(Context context, ArrayList<FotoItem> listItems) {
super(context, R.layout.listelement_foto_item, listItems);
this.context = context;
this.posts = listItems;
SEDB = new CalendarDB(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(context);
v = vi.inflate(R.layout.listelement_foto_item, null);
}
final FotoItem fotoItem = getItem(position);
if (fotoItem == null) {
Log.d("check", "getView: ist null");
}
if (fotoItem != null) {
Log.d("check", "getView: ist nicht null");
TextView user = (TextView) v.findViewById(R.id.name_foto_user);
user.setText(fotoItem.getUser());
ImageView foto = (ImageView) v.findViewById(R.id.foto_view);
TextView user_commentary = (TextView) v.findViewById(R.id.foto_user_commentary);
final ImageButton thumbUp = (ImageButton) v.findViewById(R.id.foto_thumb_up);
final ImageButton commentaryButton = (ImageButton) v.findViewById(R.id.foto_commentary_button);
final TextView thumbCount = (TextView) v.findViewById(R.id.foto_thumbcount);
final EditText commentary = (EditText) v.findViewById(R.id.commentary_box);
final ImageView shareButton = (ImageView) v.findViewById(R.id.foto_share_button);
final LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.layout_invisbile);
final Button sendButton = (Button) v.findViewById(R.id.button_edit_add_comment);
final ListView commentBox = (ListView) v.findViewById(R.id.listview_foto_commentary);
final TextView avatar = (TextView) v.findViewById(R.id.foto_avatar);
if (fotoItem.getUser()!= null) {
avatar.setText(Character.toString(fotoItem.getUser().charAt(0)));
}
comments = new ArrayList<>();
commentaryAdapter = new CommentaryAdapter(context, comments);
commentBox.setAdapter(commentaryAdapter);
Log.d("imagedb", Arrays.toString(fotoItem.getImage()));
Bitmap bitmap = BitmapFactory.decodeByteArray(fotoItem.getImage(),0,fotoItem.getImage().length);
foto.setImageBitmap(bitmap);
thumbCount.setText(Integer.toString(fotoItem.getThumbcount()));
user_commentary.setText(fotoItem.getCommentary());
Log.d("check", "Aufwand: " + fotoItem.getCommentary());
thumbUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fotoItem.addthumbUp();
thumbCount.setText(Integer.toString(fotoItem.getThumbcount()));
thumbUp.setAlpha(0.2f);
thumbUp.setClickable(false);
}
});
commentaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout.setVisibility(View.VISIBLE);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CommentaryItem commentaryItem = new CommentaryItem(getContext(),commentary.getText().toString());
comments.add(commentaryItem);
commentaryAdapter.notifyDataSetChanged();
linearLayout.setVisibility(View.INVISIBLE);
}
});
}
});
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeByteArray(fotoItem.getImage(),0,fotoItem.getImage().length);
Bitmap icon = bitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "title");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = context.getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(share, "Share Image"));
}
});
} return v;
}
}
FotoItem:
public class FotoItem erweitert PictureActivity {
private String commentary;
private byte[] image;
private String name;
private int thumbcount;
public FotoItem(String commentary, byte[] image, String name, int thumbcount){
this.commentary = commentary;
this.image = image;
this.thumbcount = thumbcount;
this.name = name;
}
public String getCommentary(){
return commentary;
}
public byte[] getImage(){
return image;
}
public void addthumbUp(){
thumbcount++;
}
public int getThumbcount(){
return thumbcount;
}
public String getUser(){
return name;
}
}
Es arbeitet ohne Datenbank gut, bis ich die Aktivität verlassen ... Danke für die Hilfe schon!