保存到内部存储的位图图像已损坏

时间:2016-06-23 04:57:08

标签: android image bitmap

我想设计一个生成QR码的应用,并让用户可以将生成的图像保存到内部存储中。我成功生成了位图并将其保存为.PNG图像,但是当我尝试从图库中打开它时,它看起来已损坏或损坏。

下面是生成位图并在ImageView(qrCode)上显示它的代码:

bitmap = encodeAsBitmap(value);
qrCode.setImageBitmap(bitmap);

Bitmap encodeAsBitmap(String str) throws WriterException {
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, WIDTH, WIDTH, null);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int w = result.getWidth();
    int h = result.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        int offset = y * w;
        for (int x = 0; x < w; x++) {
            pixels[offset + x] = result.get(x, y) ? getResources().getColor(R.color.colorBlack) :
                    getResources().getColor(R.color.colorWhite);
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, 500, 0, 0, w, h);
    return bitmap;
}

完美达到这个水平。然后,用户可以单击按钮以将此图像保存到其设备的内部存储中,这要归功于以下方法:

public void onClickSaveCode(View view) {
    String title = getResources().getString(R.string.saved_image_title_prepend) + stringDate;
    String format = getResources().getString(R.string.saved_image_format);
    String directory = getResources().getString(R.string.saved_image_directory);

    // Method call to save image
    saveImageToInternalStorage(bitmap, directory, title, format);
}

public boolean saveImageToInternalStorage(Bitmap bitmap, String directory, String title, String format) {

    ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());

    File imageDirectory = contextWrapper.getDir(directory, Context.MODE_WORLD_READABLE);

    File path = new File(imageDirectory, title + format);

    try {
        FileOutputStream fos = new FileOutputStream(path);

        // Use the compress method on the Bitmap object to write image to the OutputStream
        bitmap.compress(Bitmap.CompressFormat.PNG, QUALITY, fos);

        fos.close();

        new SingleMediaScanner(this, path);

        Toast.makeText(this, getString(R.string.save_success), Toast.LENGTH_LONG).show();

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, getString(R.string.save_failure), Toast.LENGTH_LONG).show();
        return false;
    }
}

最后是MediaScannerConnection类,用于扫描保存到设备的所有图像并将其显示在图库中:

public class SingleMediaScanner implements MediaScannerConnectionClient {

private MediaScannerConnection mSC;
private File file;

public SingleMediaScanner(Context context, File f) {
    file = f;
    mSC = new MediaScannerConnection(context, this);
    mSC.connect();
}

@Override
public void onMediaScannerConnected() {
    mSC.scanFile(file.getAbsolutePath(), null);
}

@Override
public void onScanCompleted(String path, Uri uri) {
    mSC.disconnect();
}

}

图像已保存,但它们在图库中显示为已损坏的文件。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            string filePath = System.IO.Path.Combine(path, "compressed.png");

            //Bitmap bmp = ((BitmapDrawable)imgV.Drawable).Bitmap;

            Bitmap b = newBitmap;



             FileStream ms = new FileStream(filePath, FileMode.Create);

            //FileOutputStream fos = new FileOutputStream(filePath,true);

            await b.CompressAsync(Bitmap.CompressFormat.Png, 100, ms);

            ms.Close();

            //ByteArrayOutputStream opstream = new ByteArrayOutputStream();
            //b.Compress(Bitmap.CompressFormat.Png, 100, opstream);
            //byte[] bytArray = opstream.ToByteArray();

            Toast.MakeText(Application.Context, "Compressed : " , ToastLength.Short).Show();

            imgCompress.SetImageBitmap(b);