将图像转换为字节数组

时间:2015-06-26 04:25:36

标签: java android

我只是想问一下,我有一个像Meme Generator这样的应用程序。 所以在拍照后,图片将被发送到第二个活动,

我的问题是,我在第二次活动中有Incompatible types

  

必填字节   发现了java.lang.object

你可以帮帮我吗?或者提出另一种方法来做这件事?

第一项活动:

                    FileIOManager fiom = new FileIOManager(this);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    byte[] byteArray = stream.toByteArray();
                    fiom.write("name",byteArray);

FileIOManager类:

public class FileIOManager {

    private Context context;

    public FileIOManager(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    public void write(String filename, Object file) {
        try {
            FileOutputStream fos = context.openFileOutput(filename,
                    Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(file);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Object read(String filename) {
        Object object = null;
        try {
            FileInputStream fis = context.openFileInput(filename);
            ObjectInputStream is = new ObjectInputStream(fis);
            object = is.readObject();
            is.close();

        } catch (FileNotFoundException e) {

        } catch (StreamCorruptedException e) {

        } catch (IOException e) {

        } catch (ClassNotFoundException e) {

        }

        return object;

    }

以及最后的第二项活动

    imageView = (ImageView) findViewById(R.id.imageView2);
    //byte[] byteArray = fiom.read("name");
    byte[] byteArray = fiom.read("name");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    imageView.setImageBitmap(bmp);

1 个答案:

答案 0 :(得分:0)

尝试这个,希望它会有所帮助......

public byte[] bitmapToByteArray(Bitmap bitmap)
{
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  byte[] byteArray = stream.toByteArray();
  return byteArray;
}

Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
byte[] bitmapBytes = bitmapToByteArray(bmp);
相关问题