android,如何将位图转换为文件对象

심희수*_*심희수 -1 android

我想将位图对象转换为文件对象.但是,我想将文件对象存储在内存中,而不是存储在SD卡或内部存储器中,以便我可以使用图像文件而不保存在库中.

下面的代码仅用于获取位图并将其转换为较小的图像

 public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode != RESULT_OK)
        return;

    if(requestCode == PICK_FROM_CAMERA){

        imageUri = data.getData();



        Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
        c.moveToNext();
        absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));

        Glide.with(this).load(imageUri).into(image);


        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        bitmap = BitmapFactory.decodeFile(absolutePath, options);



    }
Run Code Online (Sandbox Code Playgroud)

小智 15

希望对你有帮助

private static void persistImage(Bitmap bitmap, String name) {
File filesDir = getAppContext().getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");

OutputStream os;
try {
  os = new FileOutputStream(imageFile);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
  os.flush();
  os.close();
} catch (Exception e) {
  Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
  }
}
Run Code Online (Sandbox Code Playgroud)