Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

bitmap - saving image is taking too long android

I use this code to save an image, but it takes a long time. Sometimes it takes 10 second or more. I have decreased the bitmap size and it helps. But it also decreases the quality. How can I save bitmap in its original size without decrease in quality and fast?

String imageName = UUID.randomUUID().toString();

    String root = Environment.getExternalStorageDirectory().toString();

    File myDir = new File(root, "/removeBG");
    if (!myDir.exists()) {
        myDir.mkdirs();
    }
    String fname;
    if (imgBackground.getDrawable() != null)
        fname = imageName + ".jpg";
    else
        fname = imageName + ".png";

    File file = new File(myDir, fname);
    if (file.exists()) {
        file.delete();
    }
    try {
        file.createNewFile(); // if file already exists will do nothing
        FileOutputStream out = new FileOutputStream(file);
        bitmap = CropBitmapTransparency(bitmap);
        bitmap = resizeBitmap(bitmap, MAX_SIZE, MAX_SIZE);
        if (imgBackground.getDrawable() != null)

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

        else
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
         out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    MediaScannerConnection.scanFile(this, new String[]{file.toString()}, new String[]{file.getName()}, null);
    

I search this problem and find a soultion like this :

public static void create(OutputStream os,int cols,int rows,int r,int  g,int  b,int  a)  {     
        ImageInfo imi = new ImageInfo(cols, rows, 8, true); // 8 bits per channel, alpha
        PngWriter png = new PngWriter(os, imi);
        // just a hint to the coder to optimize compression+speed:
        png.setFilterType(FilterType.FILTER_NONE); 
        ImageLineByte iline = new ImageLineByte (imi);
        byte[] scanline = iline.getScanlineByte();// RGBA
        for (int col = 0,pos=0; col < imi.cols; col++) { 
           scanline[pos++]=(byte) r;  
           scanline[pos++]=(byte) g;
           scanline[pos++]=(byte) b;
           scanline[pos++]=(byte) a;
        }
        for (int row = 0; row < png.imgInfo.rows; row++) {
           png.writeRow(iline);
        }
        png.end();   
 }

I am too confused. I don't know how to use it.

question from:https://stackoverflow.com/questions/65871421/saving-image-is-taking-too-long-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
MediaStore.Images.Media.insertImage (getContext ().getContentResolver (),
                                    imgBitmap, nameFull.getText ().toString ().trim (), 
"");

Try this using your own variable names. This worked for me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...