Mutual conversion between local pictures and bitmap in Android

Convert Bitmap to image

/** 
 * Convert Bitmap to a local image 
 * @param path Save it as the address of the local image 
 * @param bitmap Bitmap to be converted 
 */ 
public static void saveImage(String path, Bitmap bitmap){ 
    try { 
        BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(path)); 
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos); 
        bos.flush(); 
        bos.close(); 
    } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
    } catch ( IOException e) { 
        e. printStackTrace(); 
    } 
}

Convert local image to bitmap

by way of flow

/** 
 * Convert local image to Bitmap 
 * @param path path of existing image 
 * @return 
 */ 
public static Bitmap openImage(String path){ 
    Bitmap bitmap = null; 
    try { 
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path )); 
        bitmap = BitmapFactory.decodeStream(bis); 
        bis.close(); 
    } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } 
    return bitmap; 
}

via image path

/** 
 * Convert local image to Bitmap 
 * @param path path of existing image 
 * @return 
 */ 
public static Bitmap openImage(String path){ 
    Bitmap bitmap = BitmapFactory.decodeFile(perFilePath); 
    return bitmap; 
}

This method directly passes the string of the file path, and the image of the specified path can be read into the Bitmap object.

If it is a resource file

//This method can read image information from resource files. The first parameter is usually passed to getResources(), and the second parameter is passed to the resource id of the drawable image, as follows: 
    Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),R.mipmap.aaa);

Guess you like

Origin blog.csdn.net/fromVillageCoolBoy/article/details/131182150