【问题标题】:SQL image in ListView not showing Factory nullListView 中的 SQL 图像未显示 Factory null
【发布时间】:2017-09-22 14:43:47
【问题描述】:

我正在处理存储在 SQL 数据库中的自定义 ListView 元素。我尝试将位图转换为 byteArray,然后将其保存在数据库中,但是当我离开活动并返回时,它不再存在。 我有一些错误,例如

D/skia: --- SkImageDecoder::Factory returned null

但我不知道如何解决它...

ListView的MainActivity:

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);
        }
    });
}
}

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 扩展 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;

}
}

在我离开活动之前,它可以在没有数据库的情况下正常工作... 感谢您的帮助!

【问题讨论】:

    标签: java android sqlite listview android-sqlite


    【解决方案1】:

    将图像作为字节[] 存储在数据库中似乎非常占用内存。与其将图像保存为字节 [],不如将图像保存到设备(有很多教程如何做到这一点)。然后保存到磁盘后,将图像的位置保存在数据库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      相关资源
      最近更新 更多