简体   繁体   中英

Black Image Appear when Saving?

In these days, Images make me nervous. Now, the saving image turn into black color. I saved a photo with colourful but when saving, it gets only the whole black color. I don't know what is my problem. Give me some help.

Here is my code.

BitmapFactory.Options options = new BitmapFactory.Options();
     options.inPreferredConfig = Bitmap.Config.ARGB_8888;
     Bitmap myBitmap = BitmapFactory.decodeFile(picturePath,options);
     ByteArrayOutputStream bao= new ByteArrayOutputStream();
     myBitmap.compress(Bitmap.CompressFormat.PNG,100, bao);
     byte [] ba = bao.toByteArray();
     imageSave = Base64.encodeToString(ba, Base64.DEFAULT);

It's appear like that.

 ByteArrayOutputStream bytes = new ByteArrayOutputStream();

 bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + "myImage.jpg");
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        fos = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write(bytes.toByteArray());
        fos.close();
        Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }

I got it. Here is my answers.

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

  BitmapFactory.Options options = new BitmapFactory.Options();
     options.inPreferredConfig = Bitmap.Config.ARGB_8888;
     Bitmap myBitmap = BitmapFactory.decodeFile(picturePath,options);
     imageSave = Base64.encodeToString(getBytesFromBitmap(myBitmap), Base64.NO_WRAP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM