Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将位图转换为字节,反之亦然_Android_Android Studio_Nullpointerexception - Fatal编程技术网

Android 将位图转换为字节,反之亦然

Android 将位图转换为字节,反之亦然,android,android-studio,nullpointerexception,Android,Android Studio,Nullpointerexception,我目前正在从图库中读取位图格式的图像。在将其保存到数据库时,我需要将其转换为字节,而在图像适配器类中,我需要将其转换为位图 以下是代码:-转换为字节以将其存储在数据库中 public void submitAction(View view) { /*This method creates a new post and populates it with the data added by the user. The data is then stored in the

我目前正在从图库中读取位图格式的图像。在将其保存到数据库时,我需要将其转换为字节,而在图像适配器类中,我需要将其转换为位图

以下是代码:-转换为字节以将其存储在数据库中

 public void submitAction(View view)
    {
        /*This method creates a new post and populates it with the data added by the user. The data is then stored in the database
        * using the Active Android Library.*/
        Post p = new Post();
        EditText title = (EditText) findViewById(R.id.post_title_input);
        String tit = title.getText().toString();
        EditText description = (EditText)findViewById((R.id.editText));
        String desc = description.getText().toString();
        Bitmap img = yourSelectedImage;
        p.title=tit;
        p.description=desc;
        p.section="science";
        int bytes = img.getByteCount();
        ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
        img.copyPixelsToBuffer(buffer);
        byte[] array = buffer.array();

   }
imageAdapter类中的代码-将字节[]转换为位图

   public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        /*Converting image to byte*/
        Post p = posts.get(position);
        byte[] image = p.image;
        ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        imageView.setImageBitmap(theImage);
        return imageView;
    }
运行应用程序时,它在位图theImage=BitmapFactory.decodeStreamimageStream行崩溃;并以空指针异常终止

位图到字节[]

字节[]到位图

   public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        /*Converting image to byte*/
        Post p = posts.get(position);
        byte[] image = p.image;
        ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        imageView.setImageBitmap(theImage);
        return imageView;
    }
位图到字节[]

字节[]到位图

   public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        /*Converting image to byte*/
        Post p = posts.get(position);
        byte[] image = p.image;
        ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        imageView.setImageBitmap(theImage);
        return imageView;
    }