iOS 버전과 달리 Android는 픽셀을 뽑아서 할 줄 짜리 이미지로 만듭니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
private void SaveBitmapToFileCache(Bitmap bitmap, String strFilePath)
{
    File fileCacheItem = new File(strFilePath);
    OutputStream out = null;

    try
    {
        fileCacheItem.createNewFile();
        out = new FileOutputStream(fileCacheItem);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

String path = ...
Bitmap img = BitmapFactory.decodeFile(path);

ArrayList bmSource = new ArrayList();

final int W = img.getWidth();
final int H = img.getHeight();

for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
        int pixel = img.getPixel(x, y);

        if (Color.alpha(pixel) >= 255) {// 불투명한 픽셀
            bmSource.add(pixel);
        }
    }
}

int len = bmSource.size();

Bitmap newImg = Bitmap.createBitmap(len, 1, Bitmap.Config.ARGB_8888);
for (int i = 0; i < len; ++i) {
    newImg.setPixel(i, 0, (int)bmSource.get(i));
}

File tmpFile = File.createTempFile("tmp_", ".png");
String newImgPath = tmpFile.getAbsolutePath();
SaveBitmapToFileCache(newImg, newImgPath);